Skip to content

Commit

Permalink
Refactoring the infinispan-client to use DataGridInfinispanInstance
Browse files Browse the repository at this point in the history
This is due to reason when the rest service fail the datagrid resources were not cleaned up.
  • Loading branch information
jedla97 committed Jan 15, 2025
1 parent cf3b304 commit ade89de
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 117 deletions.
5 changes: 5 additions & 0 deletions infinispan-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus.qe</groupId>
<artifactId>quarkus-test-service-infinispan</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,39 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import jakarta.inject.Inject;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;

import io.quarkus.test.bootstrap.DefaultService;
import io.quarkus.test.bootstrap.inject.OpenShiftClient;
import io.quarkus.test.utils.Command;
import io.quarkus.test.bootstrap.service.OperatorOpenShiftInfinispan;

public abstract class BaseOpenShiftInfinispanIT {
protected static final String TARGET_RESOURCES = "target/test-classes/";
protected static final String CLUSTER_SECRET = "clientcert_secret.yaml";
protected static final String CLUSTER_CONFIG = "infinispan_cluster_config.yaml";
protected static final String CLUSTER_CONFIGMAP = "infinispan_cluster_configmap.yaml";
protected static final String CONNECT_SECRET = "connect_secret.yaml";
protected static final String TLS_SECRET = "tls_secret.yaml";
protected static final String CLIENT_CERT_SECRET = TARGET_RESOURCES + "clientcert_secret.yaml";
protected static final String CLUSTER_CONFIG = TARGET_RESOURCES + "infinispan_cluster_config.yaml";
protected static final String CLUSTER_CONFIGMAP = TARGET_RESOURCES + "infinispan_cluster_configmap.yaml";
protected static final String CONNECT_SECRET = TARGET_RESOURCES + "connect_secret.yaml";
protected static final String TLS_SECRET = TARGET_RESOURCES + "tls_secret.yaml";
protected static final String CLUSTER_NAMESPACE_NAME = "datagrid-cluster";
protected static String NEW_CLUSTER_NAME = null;

private static final String ORIGIN_CLUSTER_NAME = "totally-random-infinispan-cluster-name";

@Inject
static OpenShiftClient ocClient;

@AfterAll
public static void deleteInfinispanCluster() {
deleteYaml(CLUSTER_CONFIGMAP);
deleteYaml(CLUSTER_CONFIG);
adjustYml(CLUSTER_CONFIG, NEW_CLUSTER_NAME, ORIGIN_CLUSTER_NAME);
adjustYml(CLUSTER_CONFIGMAP, NEW_CLUSTER_NAME, ORIGIN_CLUSTER_NAME);
}
@OperatorOpenShiftInfinispan(clientCertSecret = CLIENT_CERT_SECRET, clusterConfig = CLUSTER_CONFIG, clusterConfigMap = CLUSTER_CONFIGMAP, connectSecret = CONNECT_SECRET, tlsSecret = TLS_SECRET)
static DefaultService dataGridInfinispan = new DefaultService();

protected static void adjustYml(String yamlFile, String originString, String newString) {
protected static void adjustYml(Path yamlFile, String originString, String newString) {
try {
Path yamlPath = Paths.get(TARGET_RESOURCES + yamlFile);
Charset charset = StandardCharsets.UTF_8;

String yamlContent = Files.readString(yamlPath, charset);
String yamlContent = Files.readString(yamlFile, charset);
yamlContent = yamlContent.replace(originString, newString);
Files.writeString(yamlPath, yamlContent, charset);
Files.writeString(yamlFile, yamlContent, charset);
} catch (IOException ex) {
Assertions.fail("Fail to adjust YAML file. Caused by: " + ex.getMessage());
}
}

/**
* Apply the YAML file.
*/
protected static void applyYaml(String yamlFile) {
try {
new Command("oc", "apply", "-f", Paths.get(TARGET_RESOURCES + yamlFile).toString()).runAndWait();
} catch (Exception e) {
Assertions.fail("Failed to apply YAML file. Caused by: " + e.getMessage());
}
}

protected static void createInfinispanCluster() {
applyYaml(CLUSTER_SECRET);
applyYaml(CONNECT_SECRET);
applyYaml(TLS_SECRET);

// there should be unique name for every created infinispan cluster to be able parallel runs
NEW_CLUSTER_NAME = ocClient.project() + "-infinispan-cluster";

// rename infinispan cluster and configmap
adjustYml(CLUSTER_CONFIG, ORIGIN_CLUSTER_NAME, NEW_CLUSTER_NAME);
applyYaml(CLUSTER_CONFIG);
adjustYml(CLUSTER_CONFIGMAP, ORIGIN_CLUSTER_NAME, NEW_CLUSTER_NAME);
applyYaml(CLUSTER_CONFIGMAP);

try {
new Command("oc", "-n", CLUSTER_NAMESPACE_NAME, "wait", "--for", "condition=wellFormed", "--timeout=300s",
"infinispan/" + NEW_CLUSTER_NAME).runAndWait();
} catch (Exception e) {
deleteInfinispanCluster();
Assertions.fail("Fail to wait Infinispan resources to start. Caused by: " + e.getMessage());
}
}

/**
*
* Delete the YAML file.
*/
private static void deleteYaml(String yamlFile) {
try {
new Command("oc", "delete", "-f", Paths.get(TARGET_RESOURCES + yamlFile).toString()).runAndWait();
} catch (Exception e) {
Assertions.fail("Failed to delete YAML file. Caused by: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.http.HttpStatus;
import org.hamcrest.Matchers;
Expand All @@ -28,16 +29,8 @@
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class OperatorOpenShiftInfinispanCountersIT extends BaseOpenShiftInfinispanIT {

private static final AtomicBoolean CLUSTER_CREATED = new AtomicBoolean(false);

@QuarkusApplication
static RestService one = new RestService()
.onPreStart(s -> {
// prevent attempting to create cluster on restart
if (CLUSTER_CREATED.compareAndSet(false, true)) {
createInfinispanCluster();
}
});
static RestService one = new RestService();

@QuarkusApplication
static RestService two = new RestService();
Expand Down Expand Up @@ -151,8 +144,7 @@ public void testRestartInfinispanCluster() throws IOException, InterruptedExcept

incrementCountersOnValue(one, "/first-counter/increment-counters", 10);

killInfinispanCluster();
restartInfinispanCluster();
dataGridInfinispan.restart();

// try to connect back to infinispan cluster and expect no content
await().atMost(5, TimeUnit.MINUTES).untilAsserted(() -> {
Expand Down Expand Up @@ -182,8 +174,7 @@ public void testIncrementAfterRestartInfinispanCluster() throws IOException, Int

incrementCountersOnValue(one, "/first-counter/increment-counters", 10);

killInfinispanCluster();
restartInfinispanCluster();
dataGridInfinispan.restart();

// try to connect back to infinispan cluster and expect no content
await().atMost(5, TimeUnit.MINUTES).untilAsserted(() -> {
Expand Down Expand Up @@ -311,9 +302,7 @@ public void testMultipleClientDataAfterRestartInfinispanCluster() throws IOExcep
assertEquals("Cache=1 Client=1", firstClientCounters);
assertEquals("Cache=2 Client=1", secondClientCounters);

killInfinispanCluster();
restartInfinispanCluster();

dataGridInfinispan.restart();
// wait for clients to be reconnected to infinispan cluster
await().atMost(1, TimeUnit.MINUTES).untilAsserted(() -> {
one.given()
Expand Down Expand Up @@ -418,24 +407,30 @@ private void incrementCountersOnValue(RestService node, String url, int count) {
}

/**
* This is needed for `testInvokeOnFailedInfinispanCluster` instead of of restarting datagrid
* Reduces the number of infinispan cluster replicas to 0 and wait for the shutdown condition. It is done by changing
* the YAML file in the target/test-classes directory.
*/
private void killInfinispanCluster() throws IOException, InterruptedException {
adjustYml(CLUSTER_CONFIG, "replicas: 1", "replicas: 0");
applyYaml(CLUSTER_CONFIG);
Path clusterConfigPath = Path.of(System.getProperty("java.io.tmpdir"), dataGridInfinispan.getDisplayName(),
Paths.get(CLUSTER_CONFIG).getFileName().toString());
adjustYml(clusterConfigPath, "replicas: 1", "replicas: 0");
ocClient.applyInProject(clusterConfigPath, CLUSTER_NAMESPACE_NAME);
new Command("oc", "-n", CLUSTER_NAMESPACE_NAME, "wait", "--for", "condition=gracefulShutdown", "--timeout=300s",
"infinispan/" + NEW_CLUSTER_NAME).runAndWait();
"infinispan/" + dataGridInfinispan.getDisplayName()).runAndWait();
}

/**
* This is needed for `testInvokeOnFailedInfinispanCluster` instead of of restarting datagrid
* The number of replicas is increased back to value 1 the same way as in "killInfinispanCluster()" method. The wait command
* expects "wellFormed" condition in Infinispan cluster status.
*/
private void restartInfinispanCluster() throws IOException, InterruptedException {
adjustYml(CLUSTER_CONFIG, "replicas: 0", "replicas: 1");
applyYaml(CLUSTER_CONFIG);
Path clusterConfigPath = Path.of(System.getProperty("java.io.tmpdir"), dataGridInfinispan.getDisplayName(),
Paths.get(CLUSTER_CONFIG).getFileName().toString());
adjustYml(clusterConfigPath, "replicas: 0", "replicas: 1");
ocClient.applyInProject(clusterConfigPath, CLUSTER_NAMESPACE_NAME);
new Command("oc", "-n", CLUSTER_NAMESPACE_NAME, "wait", "--for", "condition=wellFormed", "--timeout=360s",
"infinispan/" + NEW_CLUSTER_NAME).runAndWait();
"infinispan/" + dataGridInfinispan.getDisplayName()).runAndWait();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public class OperatorOpenShiftInfinispanObjectsIT extends BaseOpenShiftInfinispa
new ShopItem("Item 5", 500, ShopItem.Type.MECHANICAL));

@QuarkusApplication
static RestService one = new RestService()
.onPreStart(s -> createInfinispanCluster());
static RestService one = new RestService();

@AfterEach
public void afterEach() {
Expand Down

This file was deleted.

0 comments on commit ade89de

Please sign in to comment.