Skip to content

Commit

Permalink
Add API for accessing node logs in test clusters framework (elastic#9…
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-vieira committed Jul 14, 2023
1 parent a5a7525 commit 6b6a855
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.test.cluster.util.Version;

import java.io.Closeable;
import java.io.InputStream;

/**
* A handle to an {@link ElasticsearchCluster}.
Expand Down Expand Up @@ -127,4 +128,9 @@ public interface ClusterHandle extends Closeable {
* Cleans up any resources created by this cluster.
*/
void close();

/**
* Returns an {@link InputStream} for the given node log.
*/
InputStream getNodeLog(int index, LogType logType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.io.InputStream;
import java.util.function.Supplier;

public class DefaultElasticsearchCluster<S extends ClusterSpec, H extends ClusterHandle> implements ElasticsearchCluster {
Expand Down Expand Up @@ -137,6 +138,12 @@ public void upgradeToVersion(Version version) {
handle.upgradeToVersion(version);
}

@Override
public InputStream getNodeLog(int index, LogType logType) {
checkHandle();
return handle.getNodeLog(index, logType);
}

private void checkHandle() {
if (handle == null) {
throw new IllegalStateException("Cluster handle has not been initialized. Did you forget the @ClassRule annotation?");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.test.cluster;

public enum LogType {
SERVER("%s.log"),
SERVER_JSON("%s_server.json");

private final String filenameFormat;

LogType(String filenameFormat) {
this.filenameFormat = filenameFormat;
}

public String resolveFilename(String clusterName) {
return filenameFormat.formatted(clusterName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.test.cluster.ClusterFactory;
import org.elasticsearch.test.cluster.LogType;
import org.elasticsearch.test.cluster.local.LocalClusterSpec.LocalNodeSpec;
import org.elasticsearch.test.cluster.local.distribution.DistributionDescriptor;
import org.elasticsearch.test.cluster.local.distribution.DistributionResolver;
Expand Down Expand Up @@ -229,6 +230,20 @@ public long getPid() {
return process.pid();
}

public InputStream getLog(LogType logType) {
Path logFile = logsDir.resolve(logType.resolveFilename(spec.getCluster().getName()));
if (Files.exists(logFile)) {
try {
return Files.newInputStream(logFile);
} catch (IOException e) {
LOGGER.error("Failed to read log file of type '{}' for node {} at '{}'", logType, this, logFile);
throw new RuntimeException(e);
}
}

throw new IllegalArgumentException("Log file " + logFile + " does not exist.");
}

public LocalNodeSpec getSpec() {
return spec;
}
Expand Down Expand Up @@ -354,6 +369,7 @@ private void writeConfiguration() {
try {
// Write settings to elasticsearch.yml
Map<String, String> finalSettings = new HashMap<>();
finalSettings.put("cluster.name", spec.getCluster().getName());
finalSettings.put("node.name", name);
finalSettings.put("path.repo", repoDir.toString());
finalSettings.put("path.data", dataDir.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.test.cluster.ClusterHandle;
import org.elasticsearch.test.cluster.LogType;
import org.elasticsearch.test.cluster.local.LocalClusterFactory.Node;
import org.elasticsearch.test.cluster.local.model.User;
import org.elasticsearch.test.cluster.util.ExceptionUtils;
import org.elasticsearch.test.cluster.util.Version;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
Expand Down Expand Up @@ -169,6 +171,11 @@ public void stopNode(int index) {
nodes.get(index).stop(false);
}

@Override
public InputStream getNodeLog(int index, LogType logType) {
return nodes.get(index).getLog(logType);
}

protected void waitUntilReady() {
writeUnicastHostsFile();
try {
Expand Down

0 comments on commit 6b6a855

Please sign in to comment.