-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a633910
commit 621bdc7
Showing
8 changed files
with
213 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/com/pinterest/secor/common/ShutdownHookRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.common; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Registry for shutdown hooks. | ||
* Allows running shutdown hooks by specific order by executing multiple Runnables on single shutdown hook. | ||
* | ||
* @author Paulius Dambrauskas ([email protected]) | ||
* | ||
*/ | ||
public final class ShutdownHookRegistry { | ||
private static final Logger LOG = LoggerFactory.getLogger(ShutdownHookRegistry.class); | ||
private static final Map<Integer, List<Runnable>> HOOKS = new ConcurrentHashMap<>(); | ||
|
||
private ShutdownHookRegistry() { | ||
// static class cannot be initiated | ||
} | ||
|
||
static { | ||
Runtime.getRuntime().addShutdownHook(new Thread(ShutdownHookRegistry::runHooks)); | ||
} | ||
|
||
public static void registerHook(int priority, Runnable hook) { | ||
HOOKS.computeIfAbsent(priority, key -> new ArrayList<>()).add(hook); | ||
LOG.info("Shut down hook with priority {} added to shut down hook registry", priority); | ||
} | ||
|
||
public static void runHooks() { | ||
HOOKS.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> { | ||
LOG.info("Running hooks for priority {}", entry.getKey()); | ||
entry.getValue().parallelStream().forEach(Runnable::run); | ||
}); | ||
} | ||
|
||
public static void clear() { | ||
HOOKS.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/com/pinterest/secor/io/StagingDirectoryCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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 ([email protected]) | ||
* | ||
*/ | ||
public class StagingDirectoryCleaner implements Runnable { | ||
private static final Logger LOG = LoggerFactory.getLogger(StagingDirectoryCleaner.class); | ||
|
||
private final File mStagingDir; | ||
|
||
public StagingDirectoryCleaner(String stagingPath) { | ||
this.mStagingDir = new File(stagingPath); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
FileUtils.deleteDirectory(this.mStagingDir); | ||
} catch (IOException e) { | ||
LOG.error("Failed deleting file", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/test/java/com/pinterest/secor/common/ShutdownHookRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.common; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ShutdownHookRegistryTest { | ||
|
||
@After | ||
public void cleanup() { | ||
ShutdownHookRegistry.clear(); | ||
} | ||
|
||
@Test | ||
public void testHookExecutionOrder() { | ||
List<String> results = new ArrayList<>(); | ||
ShutdownHookRegistry.registerHook(9, () -> results.add("priority9")); | ||
ShutdownHookRegistry.registerHook(1, () -> results.add("priority1")); | ||
|
||
ShutdownHookRegistry.runHooks(); | ||
|
||
assertEquals("priority1", results.get(0)); | ||
assertEquals("priority9", results.get(1)); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/com/pinterest/secor/io/StagingDirectoryCleanerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.IOException; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class StagingDirectoryCleanerTest { | ||
@Rule | ||
public TemporaryFolder folder = new TemporaryFolder(); | ||
|
||
@Test | ||
public void testCleanFolderContent() throws IOException { | ||
// Given | ||
folder.newFolder("foo"); | ||
folder.newFolder("bar"); | ||
|
||
// When | ||
new StagingDirectoryCleaner(folder.getRoot().getPath()).run(); | ||
|
||
// Then | ||
assertFalse(folder.getRoot().exists()); | ||
} | ||
} |