From 04ea99d1047723bb097cc5624288ff135d2b2c01 Mon Sep 17 00:00:00 2001 From: isadykov Date: Thu, 23 Nov 2017 11:54:17 +1100 Subject: [PATCH] More verbose logging to debug failures (refs #110) --- .../embed/postgresql/InitDbProcess.java | 31 ++++++++++--------- .../embed/postgresql/PostgresProcess.java | 6 ++-- .../ext/LogWatchStreamProcessor.java | 18 ++++++++--- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/main/java/ru/yandex/qatools/embed/postgresql/InitDbProcess.java b/src/main/java/ru/yandex/qatools/embed/postgresql/InitDbProcess.java index e30454d..5c56322 100644 --- a/src/main/java/ru/yandex/qatools/embed/postgresql/InitDbProcess.java +++ b/src/main/java/ru/yandex/qatools/embed/postgresql/InitDbProcess.java @@ -1,17 +1,17 @@ /** * Copyright (C) 2011 - * Michael Mosmann - * Martin Jöhren - * + * Michael Mosmann + * Martin Jöhren + *

* with contributions from - * konstantin-ba@github, Archimedes Trajano (trajano@github), Christian Bayer (chrbayer84@googlemail.com) - * + * konstantin-ba@github, Archimedes Trajano (trajano@github), Christian Bayer (chrbayer84@googlemail.com) + *

* Licensed 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 - * + *

+ * 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. @@ -25,22 +25,24 @@ import de.flapdoodle.embed.process.distribution.Distribution; import de.flapdoodle.embed.process.distribution.Platform; import de.flapdoodle.embed.process.extract.IExtractedFileSet; -import de.flapdoodle.embed.process.io.LogWatchStreamProcessor; import de.flapdoodle.embed.process.io.Processors; import de.flapdoodle.embed.process.io.StreamToLineProcessor; import de.flapdoodle.embed.process.io.file.Files; import de.flapdoodle.embed.process.runtime.ProcessControl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import ru.yandex.qatools.embed.postgresql.config.PostgresConfig; +import ru.yandex.qatools.embed.postgresql.ext.LogWatchStreamProcessor; import ru.yandex.qatools.embed.postgresql.ext.SubdirTempDir; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import static de.flapdoodle.embed.process.io.file.Files.createTempFile; import static java.util.Arrays.asList; +import static java.util.Collections.singleton; import static java.util.UUID.randomUUID; /** @@ -48,6 +50,7 @@ * (helper to initialize the DB) */ class InitDbProcess extends AbstractPGProcess { + private static final Logger LOGGER = LoggerFactory.getLogger(InitDbProcess.class); public InitDbProcess(Distribution distribution, PostgresConfig config, IRuntimeConfig runtimeConfig, E executable) throws IOException { super(distribution, config, runtimeConfig, executable); @@ -80,10 +83,10 @@ protected List getCommandLine(Distribution distribution, PostgresConfig @Override protected void onAfterProcessStart(ProcessControl process, IRuntimeConfig runtimeConfig) throws IOException { - ProcessOutput outputConfig = runtimeConfig.getProcessOutput(); - LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor( - "database system is ready to accept connections", - new HashSet<>(asList("[initdb error]")), StreamToLineProcessor.wrap(outputConfig.getOutput())); + final ProcessOutput outputConfig = runtimeConfig.getProcessOutput(); + final LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor( + "performing post-bootstrap initialization", + singleton("[initdb error]"), StreamToLineProcessor.wrap(outputConfig.getOutput())); Processors.connect(process.getReader(), logWatch); Processors.connect(process.getError(), StreamToLineProcessor.wrap(outputConfig.getError())); logWatch.waitForResult(getConfig().timeout().startupTimeout()); diff --git a/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresProcess.java b/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresProcess.java index 486b65f..ef2ad8c 100644 --- a/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresProcess.java +++ b/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresProcess.java @@ -118,6 +118,9 @@ private static String runCmd(boolean silent, .prepare(postgresConfig); AbstractPGProcess proc = exec.start(); logWatch.waitForResult(DEFAULT_CMD_TIMEOUT); + if (!logWatch.isInitWithSuccess() && !silent) { + LOGGER.warn("Possibly failed to run {}:\n{}", cmd.commandName(), logWatch.getOutput()); + } proc.waitFor(); return logWatch.getOutput(); } catch (IOException | InterruptedException e) { @@ -190,8 +193,7 @@ protected void onBeforeProcess(IRuntimeConfig runtimeConfig) return; } - runCmd(config, runtimeConfig, InitDb, - "Success. You can now start the database server using", emptySet()); + runCmd(config, runtimeConfig, InitDb, "Success", singleton("[initdb error]")); } @Override diff --git a/src/main/java/ru/yandex/qatools/embed/postgresql/ext/LogWatchStreamProcessor.java b/src/main/java/ru/yandex/qatools/embed/postgresql/ext/LogWatchStreamProcessor.java index f166a3a..778930f 100644 --- a/src/main/java/ru/yandex/qatools/embed/postgresql/ext/LogWatchStreamProcessor.java +++ b/src/main/java/ru/yandex/qatools/embed/postgresql/ext/LogWatchStreamProcessor.java @@ -6,7 +6,7 @@ import java.util.Set; -import static org.apache.commons.lang3.StringUtils.isEmpty; +import static java.util.Optional.ofNullable; /** * @author Ilya Sadykov @@ -18,10 +18,11 @@ public class LogWatchStreamProcessor extends de.flapdoodle.embed.process.io.LogW private final String success; private final Set failures; private volatile boolean found = false; + private volatile boolean initWithSuccess = false; public LogWatchStreamProcessor(String success, Set failures, IStreamProcessor destination) { super(success, failures, destination); - this.success = success; + this.success = ofNullable(success).orElse(""); this.failures = failures; } @@ -29,7 +30,8 @@ public LogWatchStreamProcessor(String success, Set failures, IStreamProc public void process(String block) { LOGGER.debug(block); output.append(block).append("\n"); - if (containsSuccess(block) || containsFailure(block)) { + initWithSuccess = containsSuccess(block); + if (initWithSuccess || containsFailure(block)) { synchronized (mutex) { found = true; mutex.notifyAll(); @@ -52,6 +54,7 @@ private boolean containsFailure(String block) { return false; } + @Override public void waitForResult(long timeout) { synchronized (mutex) { try { @@ -59,14 +62,19 @@ public void waitForResult(long timeout) { mutex.wait(timeout); } } catch (InterruptedException e) { + LOGGER.error("Failed to wait for the result: '{}' not found in: \n{}", success, output); e.printStackTrace(); } } } + @Override + public boolean isInitWithSuccess() { + return initWithSuccess || getOutput().contains(success); + } + @Override public String getOutput() { - String res = output.toString(); - return isEmpty(res) ? null : res; + return output.toString(); } }