-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LOGMGR-338] When archiving files, do it asynchronously. This allows …
…logging to continue while the file is compressed. https://issues.redhat.com/browse/LOGMGR-338 Signed-off-by: James R. Perkins <[email protected]>
- Loading branch information
Showing
7 changed files
with
147 additions
and
18 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
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
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 |
---|---|---|
|
@@ -33,6 +33,10 @@ | |
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentSkipListSet; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Stream; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
import org.jboss.logmanager.ExtHandler; | ||
|
@@ -46,10 +50,13 @@ | |
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
public class AbstractHandlerTest { | ||
|
||
private static final long TIMEOUT; | ||
static final File BASE_LOG_DIR; | ||
|
||
static { | ||
BASE_LOG_DIR = new File(System.getProperty("log.dir")); | ||
TIMEOUT = Long.parseLong(System.getProperty("org.jboss.test.timeout", "10")); | ||
} | ||
|
||
final static PatternFormatter FORMATTER = new PatternFormatter("%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"); | ||
|
@@ -124,6 +131,53 @@ protected ExtLogRecord createLogRecord(final org.jboss.logmanager.Level level, f | |
return new ExtLogRecord(level, String.format(format, args), getClass().getName()); | ||
} | ||
|
||
/** | ||
* Waits for all files to be rotated before exiting. | ||
* | ||
* @param archiveSuffix the type of the archive | ||
* @param expectedFiles the files which need to exist | ||
* | ||
* @throws InterruptedException if an error occurs while waiting | ||
*/ | ||
@SuppressWarnings("SameParameterValue") | ||
static void waitForRotation(final String archiveSuffix, final Path... expectedFiles) throws InterruptedException { | ||
final Set<Path> files = new ConcurrentSkipListSet<>(Set.of(expectedFiles)); | ||
final long millis = TIMEOUT * 1000L; | ||
final Thread task = new Thread(() -> { | ||
long t = millis; | ||
while (t > 0) { | ||
files.removeIf(f -> { | ||
try { | ||
if (Files.exists(f)) { | ||
// Attempt to read the archive, if it ends in an error then we assume the write is not complete | ||
if (".gz".equalsIgnoreCase(archiveSuffix)) { | ||
readAllLinesFromGzip(f); | ||
return true; | ||
} | ||
return isValidZip(f); | ||
} | ||
} catch (Throwable ignore) { | ||
ignore.printStackTrace(); | ||
} | ||
return false; | ||
}); | ||
if (files.isEmpty()) { | ||
break; | ||
} | ||
try { | ||
TimeUnit.MILLISECONDS.sleep(200L); | ||
} catch (InterruptedException ignore) { | ||
} | ||
t -= 200L; | ||
} | ||
}); | ||
task.start(); | ||
task.join(); | ||
if (!files.isEmpty()) { | ||
Assertions.fail(String.format("Failed to find all files within %d seconds. Missing files: %s", TIMEOUT, files)); | ||
} | ||
} | ||
|
||
/** | ||
* Validates that at least one line of the GZIP'd file contains the expected text. | ||
* | ||
|
@@ -229,4 +283,17 @@ private static Collection<String> readAllLinesFromGzip(final Path path) throws I | |
} | ||
return lines; | ||
} | ||
|
||
private static boolean isValidZip(final Path path) { | ||
try ( | ||
final FileSystem zipFs = FileSystems.newFileSystem(URI.create("jar:" + path.toUri().toASCIIString()), | ||
Collections.singletonMap("create", "true"))) { | ||
// Simply walk the file stream and assume if there are any entries, the ZIP is fully written | ||
try (Stream<Path> files = Files.list(zipFs.getPath("/"))) { | ||
return files.findAny().isPresent(); | ||
} | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
} | ||
} |
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
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