Skip to content

Commit

Permalink
[WFCORE-6728] Clean up the ServerReload overloaded methods a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
kabir committed Mar 21, 2024
1 parent 6c9e814 commit 923ad86
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.jboss.as.controller.operations.common.Util;
import org.jboss.as.test.integration.management.util.ServerReload;
import org.jboss.as.test.integration.security.common.CoreUtils;
import org.jboss.as.test.shared.TestSuiteEnvironment;
import org.jboss.dmr.ModelNode;
import org.junit.runner.RunWith;
import org.wildfly.core.testrunner.WildFlyRunner;
Expand Down Expand Up @@ -81,12 +80,15 @@ protected AutoCloseable configureSaslMechanismOnServer(String mechanism, boolean
final HttpMgmtConfigurator httpMgmtConfig = httpMgmtConfigBuilder.build();
httpMgmtConfig.create(client, null);

ServerReload.executeReloadAndWaitForCompletion(client, 30 * 1000, false, "remote",
TestSuiteEnvironment.getServerAddress(), PORT_NATIVE);
ServerReload.Parameters serverReloadParams = new ServerReload.Parameters()
.setTimeout(30 * 1000)
.setProtocol("remote")
.setServerPort(PORT_NATIVE);

ServerReload.executeReloadAndWaitForCompletion(client, serverReloadParams);
return () -> {
httpMgmtConfig.remove(client, null);
ServerReload.executeReloadAndWaitForCompletion(client, 30 * 1000, false, "remote",
TestSuiteEnvironment.getServerAddress(), PORT_NATIVE);
ServerReload.executeReloadAndWaitForCompletion(client, serverReloadParams);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void reloadServer() throws Exception {
try (ManagementClient managementClient = getManagementClient()) {
block(managementClient);
long timeout = SHUTDOWN_WAITING_TIME + System.currentTimeMillis();
ServerReload.executeReload(managementClient.getControllerClient(), false);
ServerReload.executeReloadAndWaitForCompletion(managementClient.getControllerClient(), false);
while (System.currentTimeMillis() < timeout) {
Thread.sleep(FREQUENCY);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.as.test.shared.TestSuiteEnvironment;
import org.jboss.as.version.Stability;
import org.jboss.dmr.ModelNode;
import org.junit.Assert;
import org.wildfly.core.testrunner.ManagementClient;
Expand Down Expand Up @@ -61,7 +62,9 @@ public static void executeReloadAndWaitForCompletion(ModelControllerClient clien
* @throws AssertionError if the reload does not complete within the timeout
*/
public static void executeReloadAndWaitForCompletion(ModelControllerClient client, boolean adminOnly) {
executeReloadAndWaitForCompletion(client, TIMEOUT, adminOnly, null, -1);
Parameters parameters = new Parameters()
.setAdminOnly(adminOnly);
executeReloadAndWaitForCompletion(client, parameters);
}

/**
Expand All @@ -72,57 +75,33 @@ public static void executeReloadAndWaitForCompletion(ModelControllerClient clien
*
* @throws AssertionError if the reload does not complete within the specified timeout
*/
public static void executeReloadAndWaitForCompletion(ModelControllerClient client, int timeout) {
executeReloadAndWaitForCompletion(client, timeout, false, null, -1);
private static void executeReloadAndWaitForCompletion(ModelControllerClient client, int timeout) {
Parameters parameters = new Parameters()
.setTimeout(timeout);
executeReloadAndWaitForCompletion(client, parameters);
}

public static void executeReloadAndWaitForCompletion(ModelControllerClient client, ModelNode reloadOp) {
executeReloadAndWaitForCompletion(client, reloadOp, TIMEOUT, null, -1);
public static void executeReloadAndWaitForCompletion(ModelControllerClient client, Parameters parameters) {
executeReload(client, parameters);
waitForLiveServerToReload(parameters);
}

public static void executeReloadAndWaitForCompletion(ModelControllerClient client, ModelNode reloadOp, int timeout, String serverAddress, int serverPort) {
executeReload(client, reloadOp);
waitForLiveServerToReload(timeout, "remote+http",
serverAddress != null ? serverAddress : TestSuiteEnvironment.getServerAddress(),
serverPort != -1 ? serverPort : TestSuiteEnvironment.getServerPort());
}

/**
* Executes a {@code reload} operation, optionally putting the server into {@code admin-only}
* running mode, and waits a configurable maximum time for the reload to complete.
*
* @param client the client to use for the request. Cannot be {@code null}
* @param timeout maximum time to wait for the reload to complete, in milliseconds
* @param adminOnly if {@code true}, the server will be reloaded in admin-only mode
* @param serverAddress if {@code null}, use {@code TestSuiteEnvironment.getServerAddress()} to create the ModelControllerClient
* @param serverPort if {@code -1}, use {@code TestSuiteEnvironment.getServerPort()} to create the ModelControllerClient
*
* @throws AssertionError if the reload does not complete within the specified timeout
*/
public static void executeReloadAndWaitForCompletion(ModelControllerClient client, int timeout, boolean adminOnly, String serverAddress, int serverPort) {
executeReload(client, adminOnly);
waitForLiveServerToReload(timeout, "remote+http",
serverAddress != null ? serverAddress : TestSuiteEnvironment.getServerAddress(),
serverPort != -1 ? serverPort : TestSuiteEnvironment.getServerPort());
}

public static void executeReloadAndWaitForCompletion(ModelControllerClient client, int timeout, boolean adminOnly, String protocol, String serverAddress, int serverPort) {
executeReload(client, adminOnly);
waitForLiveServerToReload(timeout, protocol,
serverAddress != null ? serverAddress : TestSuiteEnvironment.getServerAddress(),
serverPort != -1 ? serverPort : TestSuiteEnvironment.getServerPort());
}

public static void executeReload(ModelControllerClient client, boolean adminOnly) {
private static void executeReload(ModelControllerClient client, Parameters parameters) {
ModelNode operation = new ModelNode();
operation.get(OP_ADDR).setEmptyList();
operation.get(OP).set("reload");
operation.get("admin-only").set(adminOnly);
operation.get("admin-only").set(parameters.adminOnly);
if (parameters.stability != null) {
operation.get(OP).set("reload-enhanced");
operation.get("stability").set(parameters.stability.toString());
} else {
operation.get(OP).set("reload");
}

executeReload(client, operation);
}

public static void executeReload(ModelControllerClient client, ModelNode reloadOp) {

private static void executeReload(ModelControllerClient client, ModelNode reloadOp) {
try {
ModelNode result = client.execute(reloadOp);
Assert.assertEquals(SUCCESS, result.get(ClientConstants.OUTCOME).asString());
Expand Down Expand Up @@ -157,22 +136,22 @@ public static void reloadIfRequired(final ModelControllerClient controllerClient
}
}

private static void waitForLiveServerToReload(int timeout, String protocol, String serverAddress, int serverPort) {
private static void waitForLiveServerToReload(Parameters parameters) {
long start = System.currentTimeMillis();
ModelNode operation = new ModelNode();
operation.get(OP_ADDR).setEmptyList();
operation.get(OP).set(READ_ATTRIBUTE_OPERATION);
operation.get(NAME).set("server-state");
while (System.currentTimeMillis() - start < timeout) {
while (System.currentTimeMillis() - start < parameters.timeout) {
//do the sleep before we check, as the attribute state may not change instantly
//also reload generally takes longer than 100ms anyway
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
try {
ModelControllerClient liveClient = ModelControllerClient.Factory.create(protocol,
serverAddress, serverPort);
ModelControllerClient liveClient = ModelControllerClient.Factory.create(
parameters.protocol, parameters.serverAddress, parameters.serverPort);
try {
ModelNode result = liveClient.execute(operation);
if ("running".equals(result.get(RESULT).asString())) {
Expand Down Expand Up @@ -217,5 +196,46 @@ public void tearDown(ManagementClient managementClient) throws Exception {
executeReloadAndWaitForCompletion(managementClient.getControllerClient());
}
}

public static class Parameters {
private int timeout = TIMEOUT;
boolean adminOnly = false;
String protocol = "remote+http";
String serverAddress = TestSuiteEnvironment.getServerAddress();
int serverPort = TestSuiteEnvironment.getServerPort();

Stability stability = null;

public Parameters setTimeout(int timeout) {
this.timeout = timeout;
return this;
}

public Parameters setAdminOnly(boolean adminOnly) {
this.adminOnly = adminOnly;
return this;
}

public Parameters setProtocol(String protocol) {
this.protocol = protocol;
return this;
}

public Parameters setServerAddress(String serverAddress) {
this.serverAddress = serverAddress;
return this;
}

public Parameters setServerPort(int serverPort) {
this.serverPort = serverPort;
return this;
}

public Parameters setStability(Stability stability) {
this.stability = stability;
return this;
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.wildfly.test.stability;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.operations.common.Util;
import org.jboss.as.test.integration.management.ManagementOperations;
import org.jboss.as.test.integration.management.util.ServerReload;
import org.jboss.as.version.Stability;
import org.jboss.dmr.ModelNode;
import org.junit.Assert;
import org.junit.Assume;
import org.wildfly.core.testrunner.ManagementClient;
import org.wildfly.core.testrunner.ServerSetupTask;

import java.util.EnumSet;
import java.util.Set;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CORE_SERVICE;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.READ_OPERATION_NAMES_OPERATION;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RELOAD_ENHANCED;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.STABILITY;
import static org.jboss.as.server.controller.descriptions.ServerDescriptionConstants.SERVER_ENVIRONMENT;

/**
* For tests that need to run under a specific server stability level,
* the server setup tasks from the inner classes can be used to change the stability level of the server to the desired level.
* Once the test is done, the original stability level is restored.
*/
public abstract class StabilityServerSetupTasks implements ServerSetupTask {

private final Stability desiredStability;
private volatile Stability originalStability;


public StabilityServerSetupTasks(Stability desiredStability) {
this.desiredStability = desiredStability;
}

@Override
public void setup(ManagementClient managementClient) throws Exception {
Set<Stability> supportedStabilityLevels = getSupportedStabilityLevels();
Assume.assumeTrue(
String.format("%s is not a supported stability level", desiredStability, supportedStabilityLevels),
supportedStabilityLevels.contains(desiredStability));

Assume.assumeTrue(
"The reload-enhanced operation is not registered at this stability level",
checkReloadEnhancedOperationIsAvailable(managementClient));

reloadToDesiredStability(managementClient.getControllerClient(), desiredStability);
}

@Override
public void tearDown(ManagementClient managementClient) throws Exception {
reloadToDesiredStability(managementClient.getControllerClient(), originalStability);
}

private boolean checkReloadEnhancedOperationIsAvailable(ManagementClient managementClient) throws Exception {
ModelNode op = Util.createOperation(READ_OPERATION_NAMES_OPERATION, PathAddress.EMPTY_ADDRESS);
ModelNode result = ManagementOperations.executeOperation(managementClient.getControllerClient(), op);
for (ModelNode name : result.asList()) {
if (name.asString().equals(RELOAD_ENHANCED)) {
return true;
}
}
return false;
}

private Set<Stability> getSupportedStabilityLevels() {
// TODO WFCORE-6731 - see https://github.com/wildfly/wildfly-core/pull/5895#discussion_r1520489808
// This information will be available in a management operation, For now just return a hardcoded set
return EnumSet.allOf(Stability.class);
}

private Stability reloadToDesiredStability(ModelControllerClient client, Stability stability) throws Exception {
// Check the stability
Stability currentStability = readCurrentStability(client);
if (originalStability == null) {
originalStability = currentStability;
}

// The stability parameter for the reload opration is only registered below the default level
Assume.assumeFalse("Can't reload to a different stability when server stability level is default", currentStability == Stability.DEFAULT && stability != Stability.DEFAULT);

if (currentStability == stability) {
return originalStability;
}

//Reload the server to the desired stability level
ServerReload.Parameters parameters = new ServerReload.Parameters()
.setStability(stability);
// Execute the reload
ServerReload.executeReloadAndWaitForCompletion(client, parameters);

Stability reloadedStability = readCurrentStability(client);
Assert.assertEquals(stability, reloadedStability);
return originalStability;
}

private Stability readCurrentStability(ModelControllerClient client) throws Exception {
ModelNode op = Util.getReadAttributeOperation(PathAddress.pathAddress(CORE_SERVICE, SERVER_ENVIRONMENT), STABILITY);
ModelNode result = ManagementOperations.executeOperation(client, op);
return Stability.fromString(result.asString());
}

/**
* A server setup task that sets the server stability to the default level.
*/
public static class Default extends StabilityServerSetupTasks {
public Default() {
super(Stability.DEFAULT);
}
}

/**
* A server setup task that sets the server stability to the community level.
*/
public static class Community extends StabilityServerSetupTasks {
public Community() {
super(Stability.COMMUNITY);
}
}

/**
* A server setup task that sets the server stability to the preview level.
*/
public static class Preview extends StabilityServerSetupTasks {
public Preview() {
super(Stability.PREVIEW);
}
}

/**
* A server setup task that sets the server stability to the experimental level.
*/
public static class Experimental extends StabilityServerSetupTasks {
public Experimental() {
super(Stability.EXPERIMENTAL);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public void setup(final ManagementClient managementClient) throws Exception {
executeForSuccess(client, compositeOp.build());

// Use the current client to execute the reload, but the native client to ensure the reload is complete
ServerReload.executeReloadAndWaitForCompletion(client, ServerReload.TIMEOUT, false, protocol, host, port);
ServerReload.executeReloadAndWaitForCompletion(client, new ServerReload.Parameters()
.setProtocol(protocol)
.setServerAddress(host)
.setServerPort(port)
);
}

@Override
Expand Down

0 comments on commit 923ad86

Please sign in to comment.