|
|
|
@ -12,7 +12,6 @@ import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
|
|
|
|
|
import org.elasticsearch.action.admin.cluster.state.ClusterStateAction;
|
|
|
|
|
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
|
|
|
|
|
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
|
|
|
|
|
import org.elasticsearch.client.ElasticsearchClient;
|
|
|
|
|
import org.elasticsearch.client.support.AbstractClient;
|
|
|
|
|
import org.elasticsearch.cluster.health.ClusterHealthStatus;
|
|
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
|
@ -33,8 +32,7 @@ import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.nio.file.SimpleFileVisitor;
|
|
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
|
@ -42,11 +40,7 @@ public class TestExtension implements ParameterResolver, BeforeEachCallback, Aft
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LogManager.getLogger("test");
|
|
|
|
|
|
|
|
|
|
private static final Random random = new Random();
|
|
|
|
|
|
|
|
|
|
private static final char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz").toCharArray();
|
|
|
|
|
|
|
|
|
|
private static final String key = "es-instance";
|
|
|
|
|
private static final String key = "es-instance-";
|
|
|
|
|
|
|
|
|
|
private static final AtomicInteger count = new AtomicInteger(0);
|
|
|
|
|
|
|
|
|
@ -62,73 +56,35 @@ public class TestExtension implements ParameterResolver, BeforeEachCallback, Aft
|
|
|
|
|
@Override
|
|
|
|
|
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
|
|
|
|
|
throws ParameterResolutionException {
|
|
|
|
|
// initialize new helper here, increase counter
|
|
|
|
|
return extensionContext.getParent().get().getStore(ns)
|
|
|
|
|
.getOrComputeIfAbsent(key + count.incrementAndGet(), key -> create(), Helper.class);
|
|
|
|
|
return extensionContext.getParent().isPresent() ?
|
|
|
|
|
extensionContext.getParent().get().getStore(ns).getOrComputeIfAbsent(key + count.incrementAndGet(), key -> create(), Helper.class) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void beforeEach(ExtensionContext extensionContext) throws Exception {
|
|
|
|
|
Helper helper = extensionContext.getParent().get().getStore(ns)
|
|
|
|
|
.getOrComputeIfAbsent(key + count.get(), key -> create(), Helper.class);
|
|
|
|
|
Helper helper = extensionContext.getParent().isPresent() ?
|
|
|
|
|
extensionContext.getParent().get().getStore(ns).getOrComputeIfAbsent(key + count.get(), key -> create(), Helper.class) : null;
|
|
|
|
|
Objects.requireNonNull(helper);
|
|
|
|
|
logger.info("starting cluster with helper " + helper + " at " + helper.getHome());
|
|
|
|
|
helper.startNode("1");
|
|
|
|
|
NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().transport(true);
|
|
|
|
|
NodesInfoResponse response = helper.client("1"). execute(NodesInfoAction.INSTANCE, nodesInfoRequest).actionGet();
|
|
|
|
|
Object obj = response.iterator().next().getTransport().getAddress()
|
|
|
|
|
.publishAddress();
|
|
|
|
|
String host = null;
|
|
|
|
|
int port = 0;
|
|
|
|
|
if (obj instanceof InetSocketTransportAddress) {
|
|
|
|
|
InetSocketTransportAddress address = (InetSocketTransportAddress) obj;
|
|
|
|
|
host = address.address().getHostName();
|
|
|
|
|
port = address.address().getPort();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
ClusterHealthResponse healthResponse = helper.client("1").execute(ClusterHealthAction.INSTANCE,
|
|
|
|
|
new ClusterHealthRequest().waitForStatus(ClusterHealthStatus.GREEN)
|
|
|
|
|
.timeout(TimeValue.timeValueSeconds(30))).actionGet();
|
|
|
|
|
if (healthResponse != null && healthResponse.isTimedOut()) {
|
|
|
|
|
throw new IOException("cluster state is " + healthResponse.getStatus().name()
|
|
|
|
|
+ ", from here on, everything will fail!");
|
|
|
|
|
}
|
|
|
|
|
} catch (ElasticsearchTimeoutException e) {
|
|
|
|
|
throw new IOException("cluster does not respond to health request, cowardly refusing to continue");
|
|
|
|
|
}
|
|
|
|
|
ClusterStateRequest clusterStateRequest = new ClusterStateRequest().all();
|
|
|
|
|
ClusterStateResponse clusterStateResponse =
|
|
|
|
|
helper.client("1").execute(ClusterStateAction.INSTANCE, clusterStateRequest).actionGet();
|
|
|
|
|
logger.info("cluster name = {}", clusterStateResponse.getClusterName().value());
|
|
|
|
|
logger.info("host = {} port = {}", host, port);
|
|
|
|
|
helper.startNode();
|
|
|
|
|
helper.greenHealth();
|
|
|
|
|
logger.info("cluser name = {}", helper.clusterName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void afterEach(ExtensionContext extensionContext) throws Exception {
|
|
|
|
|
Helper helper = extensionContext.getParent().get().getStore(ns)
|
|
|
|
|
.getOrComputeIfAbsent(key + count.get(), key -> create(), Helper.class);
|
|
|
|
|
closeNodes(helper);
|
|
|
|
|
deleteFiles(Paths.get(helper.getHome() + "/data"));
|
|
|
|
|
logger.info("data files wiped");
|
|
|
|
|
Thread.sleep(2000L); // let OS commit changes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void closeNodes(Helper helper) throws IOException {
|
|
|
|
|
logger.info("closing all clients");
|
|
|
|
|
for (AbstractClient client : helper.clients.values()) {
|
|
|
|
|
client.close();
|
|
|
|
|
}
|
|
|
|
|
logger.info("closing all nodes");
|
|
|
|
|
for (Node node : helper.nodes.values()) {
|
|
|
|
|
if (node != null) {
|
|
|
|
|
node.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
logger.info("all nodes closed");
|
|
|
|
|
Helper helper = extensionContext.getParent().isPresent() ?
|
|
|
|
|
extensionContext.getParent().get().getStore(ns).getOrComputeIfAbsent(key + count.get(), key -> create(), Helper.class) : null;
|
|
|
|
|
Objects.requireNonNull(helper);
|
|
|
|
|
helper.closeNodes();
|
|
|
|
|
deleteFiles(Paths.get(helper.getHome()));
|
|
|
|
|
logger.info("files wiped");
|
|
|
|
|
Thread.sleep(1000L); // let OS commit changes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void deleteFiles(Path directory) throws IOException {
|
|
|
|
|
if (Files.exists(directory)) {
|
|
|
|
|
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
|
|
|
|
|
Files.walkFileTree(directory, new SimpleFileVisitor<>() {
|
|
|
|
|
@Override
|
|
|
|
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
|
|
|
Files.delete(file);
|
|
|
|
@ -146,7 +102,8 @@ public class TestExtension implements ParameterResolver, BeforeEachCallback, Aft
|
|
|
|
|
|
|
|
|
|
private Helper create() {
|
|
|
|
|
Helper helper = new Helper();
|
|
|
|
|
helper.setHome(System.getProperty("path.home") + "/" + helper.randomString(8));
|
|
|
|
|
String home = System.getProperty("path.home", "build/elxnode");
|
|
|
|
|
helper.setHome(home + "/" + helper.randomString(8));
|
|
|
|
|
helper.setClusterName("test-cluster-" + helper.randomString(8));
|
|
|
|
|
logger.info("cluster: " + helper.getClusterName() + " home: " + helper.getHome());
|
|
|
|
|
return helper;
|
|
|
|
@ -158,9 +115,13 @@ public class TestExtension implements ParameterResolver, BeforeEachCallback, Aft
|
|
|
|
|
|
|
|
|
|
String cluster;
|
|
|
|
|
|
|
|
|
|
Map<String, Node> nodes = new HashMap<>();
|
|
|
|
|
String host;
|
|
|
|
|
|
|
|
|
|
int port;
|
|
|
|
|
|
|
|
|
|
Map<String, AbstractClient> clients = new HashMap<>();
|
|
|
|
|
Node node;
|
|
|
|
|
|
|
|
|
|
AbstractClient client;
|
|
|
|
|
|
|
|
|
|
void setHome(String home) {
|
|
|
|
|
this.home = home;
|
|
|
|
@ -180,18 +141,23 @@ public class TestExtension implements ParameterResolver, BeforeEachCallback, Aft
|
|
|
|
|
|
|
|
|
|
Settings getNodeSettings() {
|
|
|
|
|
return Settings.builder()
|
|
|
|
|
.put("name", "elx-client") // for Threadpool name
|
|
|
|
|
.put("name", "elx-client") // for threadpool name
|
|
|
|
|
.put("cluster.name", getClusterName())
|
|
|
|
|
.put("path.home", getHome())
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void startNode(String id) {
|
|
|
|
|
buildNode(id).start();
|
|
|
|
|
void startNode() {
|
|
|
|
|
buildNode().start();
|
|
|
|
|
NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().transport(true);
|
|
|
|
|
NodesInfoResponse response = client.execute(NodesInfoAction.INSTANCE, nodesInfoRequest).actionGet();
|
|
|
|
|
Object obj = response.iterator().next().getTransport().getAddress().publishAddress();
|
|
|
|
|
if (obj instanceof InetSocketTransportAddress) {
|
|
|
|
|
InetSocketTransportAddress address = (InetSocketTransportAddress) obj;
|
|
|
|
|
host = address.address().getHostName();
|
|
|
|
|
port = address.address().getPort();
|
|
|
|
|
logger.info("host = {} port = {}", host, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ElasticsearchClient client(String id) {
|
|
|
|
|
return clients.get(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String randomString(int len) {
|
|
|
|
@ -203,16 +169,51 @@ public class TestExtension implements ParameterResolver, BeforeEachCallback, Aft
|
|
|
|
|
return new String(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Node buildNode(String id) {
|
|
|
|
|
Node buildNode() {
|
|
|
|
|
String id = "1";
|
|
|
|
|
Settings nodeSettings = Settings.builder()
|
|
|
|
|
.put(getNodeSettings())
|
|
|
|
|
.put("node.name", id)
|
|
|
|
|
.build();
|
|
|
|
|
Node node = new MockNode(nodeSettings);
|
|
|
|
|
AbstractClient client = (AbstractClient) node.client();
|
|
|
|
|
nodes.put(id, node);
|
|
|
|
|
clients.put(id, client);
|
|
|
|
|
node = new MockNode(nodeSettings);
|
|
|
|
|
client = (AbstractClient) node.client();
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void closeNodes() {
|
|
|
|
|
if (client != null) {
|
|
|
|
|
logger.info("closing client");
|
|
|
|
|
client.close();
|
|
|
|
|
}
|
|
|
|
|
if (node != null) {
|
|
|
|
|
logger.info("closing all nodes");
|
|
|
|
|
node.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void greenHealth() throws IOException {
|
|
|
|
|
try {
|
|
|
|
|
ClusterHealthResponse healthResponse = client.execute(ClusterHealthAction.INSTANCE,
|
|
|
|
|
new ClusterHealthRequest().waitForStatus(ClusterHealthStatus.GREEN)
|
|
|
|
|
.timeout(TimeValue.timeValueSeconds(30))).actionGet();
|
|
|
|
|
if (healthResponse != null && healthResponse.isTimedOut()) {
|
|
|
|
|
throw new IOException("cluster state is " + healthResponse.getStatus().name()
|
|
|
|
|
+ ", from here on, everything will fail!");
|
|
|
|
|
}
|
|
|
|
|
} catch (ElasticsearchTimeoutException e) {
|
|
|
|
|
throw new IOException("cluster does not respond to health request, cowardly refusing to continue");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String clusterName() {
|
|
|
|
|
ClusterStateRequest clusterStateRequest = new ClusterStateRequest().all();
|
|
|
|
|
ClusterStateResponse clusterStateResponse =
|
|
|
|
|
client.execute(ClusterStateAction.INSTANCE, clusterStateRequest).actionGet();
|
|
|
|
|
return clusterStateResponse.getClusterName().value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Random random = new Random();
|
|
|
|
|
|
|
|
|
|
private static final char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz").toCharArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|