Skip to content

Commit

Permalink
fix leak with files not cleaning up correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
fjy authored and xvrl committed Nov 13, 2014
1 parent 10b7ca9 commit 3ef21bf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.metamx.common.logger.Logger;
import io.druid.segment.IndexIO;
import io.druid.segment.QueryableIndex;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
Expand All @@ -40,13 +39,6 @@ public QueryableIndex factorize(File parentDir) throws SegmentLoadingException
return IndexIO.loadIndex(parentDir);
}
catch (IOException e) {
log.warn(e, "Got exception!!!! Going to delete parentDir[%s]", parentDir);
try {
FileUtils.deleteDirectory(parentDir);
}
catch (IOException e2) {
log.error(e, "Problem deleting parentDir[%s]", parentDir);
}
throw new SegmentLoadingException(e, "%s", e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class OmniSegmentLoader implements SegmentLoader

private final List<StorageLocation> locations;

private final Object lock = new Object();

@Inject
public OmniSegmentLoader(
Map<String, DataSegmentPuller> pullers,
Expand Down Expand Up @@ -118,16 +120,35 @@ public File getSegmentFiles(DataSegment segment) throws SegmentLoadingException
}

File storageDir = new File(loc.getPath(), DataSegmentPusherUtil.getStorageDir(segment));
if (!storageDir.mkdirs()) {
log.debug("Unable to make parent file[%s]", storageDir);

// We use a marker to prevent the case where a segment is downloaded, but before the download completes,
// the parent directories of the segment are removed
final File downloadStartMarker = new File(storageDir, "downloadStartMarker");
synchronized (lock) {
if (!storageDir.mkdirs()) {
log.debug("Unable to make parent file[%s]", storageDir);
}
try {
downloadStartMarker.createNewFile();
}
catch (IOException e) {
throw new SegmentLoadingException("Unable to create marker file for [%s]", storageDir);
}
}

getPuller(segment.getLoadSpec()).getSegmentFiles(segment, storageDir);

try {
FileUtils.deleteDirectory(downloadStartMarker);
}
catch (Exception e) {
throw new SegmentLoadingException("Unable to remove marker file for [%s]", storageDir);
}

loc.addSegment(segment);

retVal = storageDir;
}
else {
} else {
retVal = new File(loc.getPath(), DataSegmentPusherUtil.getStorageDir(segment));
}

Expand All @@ -151,9 +172,10 @@ public void cleanup(DataSegment segment) throws SegmentLoadingException
}

try {
// Druid creates folders of the form dataSource/interval/version/partitionNum.
// We need to clean up all these directories if they are all empty.
File cacheFile = new File(loc.getPath(), DataSegmentPusherUtil.getStorageDir(segment));
log.info("Deleting directory[%s]", cacheFile);
FileUtils.deleteDirectory(cacheFile);
cleanupCacheFiles(loc.getPath(), cacheFile);
loc.removeSegment(segment);
}
catch (IOException e) {
Expand All @@ -172,4 +194,25 @@ private DataSegmentPuller getPuller(Map<String, Object> loadSpec) throws Segment

return loader;
}

public void cleanupCacheFiles(File baseFile, File cacheFile) throws IOException
{
if (cacheFile.equals(baseFile)) {
return;
}

synchronized (lock) {
log.info("Deleting directory[%s]", cacheFile);
try {
FileUtils.deleteDirectory(cacheFile);
}
catch (Exception e) {
log.error("Unable to remove file[%s]", cacheFile);
}
}

if (cacheFile.getParentFile() != null && cacheFile.getParentFile().listFiles().length == 0) {
cleanupCacheFiles(baseFile, cacheFile.getParentFile());
}
}
}

0 comments on commit 3ef21bf

Please sign in to comment.