diff --git a/x-pack/plugin/ml/log-config-creator/src/main/java/org/elasticsearch/xpack/ml/configcreator/LogConfigCreator.java b/x-pack/plugin/ml/log-config-creator/src/main/java/org/elasticsearch/xpack/ml/configcreator/LogConfigCreator.java index a33d2d4e93388..92a4be3ab8bd2 100644 --- a/x-pack/plugin/ml/log-config-creator/src/main/java/org/elasticsearch/xpack/ml/configcreator/LogConfigCreator.java +++ b/x-pack/plugin/ml/log-config-creator/src/main/java/org/elasticsearch/xpack/ml/configcreator/LogConfigCreator.java @@ -24,6 +24,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.Comparator; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -155,10 +157,11 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception { throw new UserException(ExitCodes.USAGE, "[" + file + "] does not exist or is not a file"); } + ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1); try { LogConfigWriter logConfigWriter = new LogConfigWriter(terminal, filebeatModulePath, file.toAbsolutePath().normalize().toString(), indexName, typeName, elasticsearchHost, logstashHost, timezone); - FileStructureFinderManager structureFinderManager = new FileStructureFinderManager(); + FileStructureFinderManager structureFinderManager = new FileStructureFinderManager(scheduler); FileStructureFinder structureFinder = structureFinderManager.findFileStructure(sampleLines, Files.newInputStream(file)); FileStructure structure = structureFinder.getStructure(); for (String reason : structure.getExplanation()) { @@ -167,6 +170,8 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception { logConfigWriter.writeConfigs(structure, structureFinder.getSampleMessages(), outputDirectory); } catch (IllegalArgumentException | IOException e) { throw new UserException(ExitCodes.DATA_ERROR, "Cannot determine format of file [" + file + "]: " + e.getMessage()); + } finally { + scheduler.shutdown(); } } diff --git a/x-pack/plugin/ml/log-config-creator/src/test/java/org/elasticsearch/xpack/ml/configcreator/LogConfigCreatorTests.java b/x-pack/plugin/ml/log-config-creator/src/test/java/org/elasticsearch/xpack/ml/configcreator/LogConfigCreatorTests.java index 188bd92a746dd..2501089e28b25 100644 --- a/x-pack/plugin/ml/log-config-creator/src/test/java/org/elasticsearch/xpack/ml/configcreator/LogConfigCreatorTests.java +++ b/x-pack/plugin/ml/log-config-creator/src/test/java/org/elasticsearch/xpack/ml/configcreator/LogConfigCreatorTests.java @@ -7,6 +7,7 @@ import org.elasticsearch.xpack.ml.filestructurefinder.FileStructureFinder; import org.elasticsearch.xpack.ml.filestructurefinder.FileStructureFinderManager; +import org.junit.After; import org.junit.Before; import java.io.ByteArrayInputStream; @@ -14,6 +15,8 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; public class LogConfigCreatorTests extends LogConfigCreatorTestCase { @@ -30,16 +33,23 @@ public class LogConfigCreatorTests extends LogConfigCreatorTestCase { private static final String INDEX_MAPPINGS_CONSOLE = TEST_TYPE + "-index-mappings.console"; private static final String INDEX_MAPPINGS_SH = TEST_TYPE + "-index-mappings.sh"; + private ScheduledExecutorService scheduler; private FileStructureFinderManager structureFinderManager; private LogConfigWriter logConfigWriter; @Before public void setup() throws IOException { - structureFinderManager = new FileStructureFinderManager(); + scheduler = new ScheduledThreadPoolExecutor(1); + structureFinderManager = new FileStructureFinderManager(scheduler); logConfigWriter = new LogConfigWriter(TEST_TERMINAL, null, TEST_FILE_NAME, TEST_INDEX_NAME, TEST_TYPE, randomFrom(POSSIBLE_HOSTNAMES), randomFrom(POSSIBLE_HOSTNAMES), "UTC"); } + @After + public void shutdownScheduler() { + scheduler.shutdown(); + } + public void testFindLogFileFormatGivenJson() throws Exception { Path outputDirectory = createTempDir(); diff --git a/x-pack/plugin/ml/log-config-creator/src/test/java/org/elasticsearch/xpack/ml/configcreator/LogConfigWriterTests.java b/x-pack/plugin/ml/log-config-creator/src/test/java/org/elasticsearch/xpack/ml/configcreator/LogConfigWriterTests.java index 50d245f667ae7..3c5089d741fb5 100644 --- a/x-pack/plugin/ml/log-config-creator/src/test/java/org/elasticsearch/xpack/ml/configcreator/LogConfigWriterTests.java +++ b/x-pack/plugin/ml/log-config-creator/src/test/java/org/elasticsearch/xpack/ml/configcreator/LogConfigWriterTests.java @@ -8,6 +8,8 @@ import org.elasticsearch.xpack.ml.filestructurefinder.FileStructureFinder; import org.elasticsearch.xpack.ml.filestructurefinder.FileStructureFinderManager; import org.elasticsearch.xpack.ml.filestructurefinder.FileStructureUtils; +import org.junit.After; +import org.junit.Before; import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; @@ -15,13 +17,27 @@ import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; public class LogConfigWriterTests extends LogConfigCreatorTestCase { - private FileStructureFinderManager structureFinderManager = new FileStructureFinderManager(); + private ScheduledExecutorService scheduler; + private FileStructureFinderManager structureFinderManager; + + @Before + public void setup() { + scheduler = new ScheduledThreadPoolExecutor(1); + structureFinderManager = new FileStructureFinderManager(scheduler); + } + + @After + public void shutdownScheduler() { + scheduler.shutdown(); + } public void testBestLogstashQuoteFor() { assertEquals("\"", LogConfigWriter.bestLogstashQuoteFor("normal"));