Skip to content

Commit

Permalink
Rework cleanup process on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
pdambrauskas committed Jul 10, 2020
1 parent a633910 commit 4baeb11
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
5 changes: 0 additions & 5 deletions src/main/java/com/pinterest/secor/common/FileRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ public FileWriter getOrCreateWriter(LogFilePath path, CompressionCodec codec)
mWriters.put(path, writer);
mCreationTimes.put(path, System.currentTimeMillis() / 1000L);
LOG.debug("created writer for path {}", path.getLogFilePath());
LOG.debug("Register deleteOnExit for path {}", path.getLogFilePath());
FileUtil.deleteOnExit(path.getLogFileParentDir());
FileUtil.deleteOnExit(path.getLogFileDir());
FileUtil.deleteOnExit(path.getLogFilePath());
FileUtil.deleteOnExit(path.getLogFileCrcPath());
}
return writer;
}
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/pinterest/secor/io/StagingCleaner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.pinterest.secor.io;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Runnable used to delete staging folder content.
* Deletes folders content, while keeping folder itself.
*
* @author Paulius Dambrauskas (p.dambrauskas@gmail.com)
*
*/
public class StagingCleaner implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(StagingCleaner.class);

private final File mStagingDir;

public StagingCleaner(String stagingPath) {
this.mStagingDir = new File(stagingPath);
}

@Override
public void run() {
for (File file : mStagingDir.listFiles()) {
try {
FileUtils.deleteDirectory(file);
} catch (IOException e) {
LOG.error("Failed deleting {}", file, e);
}
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/pinterest/secor/main/ConsumerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.pinterest.secor.common.OstrichAdminService;
import com.pinterest.secor.common.SecorConfig;
import com.pinterest.secor.consumer.Consumer;
import com.pinterest.secor.io.StagingCleaner;
import com.pinterest.secor.tools.LogFileDeleter;
import com.pinterest.secor.util.FileUtil;
import com.pinterest.secor.util.RateLimitUtil;
Expand Down Expand Up @@ -54,6 +55,10 @@ public static void main(String[] args) {
}
try {
SecorConfig config = SecorConfig.load();
sun.misc.SharedSecrets.getJavaLangAccess()
.registerShutdownHook(2 /* Shutdown hook invocation order */,
true,
new Thread(new StagingCleaner(config.getLocalPath())));
OstrichAdminService ostrichService = new OstrichAdminService(config.getOstrichPort());
ostrichService.start();
FileUtil.configure(config);
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/pinterest/secor/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,6 @@ public static void delete(String path) throws IOException {
}
}

public static void deleteOnExit(String path) {
File file = new File(path);
file.deleteOnExit();
}

public static void moveToCloud(String srcLocalPath, String dstCloudPath) throws IOException {
Path srcPath = new Path(srcLocalPath);
Path dstPath = new Path(dstCloudPath);
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/com/pinterest/secor/io/StagingCleanerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.pinterest.secor.io;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class StagingCleanerTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void testCleanFolderContent() throws IOException {
// Given
folder.newFolder("foo");
folder.newFolder("bar");

// When
new StagingCleaner(folder.getRoot().getPath()).run();

// Then
assertTrue(folder.getRoot().exists());
assertEquals(0, folder.getRoot().listFiles().length);
}
}

0 comments on commit 4baeb11

Please sign in to comment.