Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W-17114229: Fix broken deployment tests #14160

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import static org.mule.runtime.core.internal.util.MuleContainerUtils.getMuleAppsDir;
import static org.mule.runtime.deployment.model.internal.DefaultRegionPluginClassLoadersFactory.PLUGIN_CLASSLOADER_IDENTIFIER;
import static org.mule.runtime.deployment.model.internal.DefaultRegionPluginClassLoadersFactory.getArtifactPluginId;
import static org.mule.runtime.module.deployment.impl.internal.util.DeploymentPropertiesUtils.getPersistedDeploymentProperties;

import static java.lang.String.format;
import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.toList;

import static com.google.common.collect.Maps.fromProperties;
import static org.apache.commons.lang3.StringUtils.removeEndIgnoreCase;

import org.mule.runtime.api.lock.LockFactory;
import org.mule.runtime.api.memory.management.MemoryManagementService;
Expand All @@ -34,6 +36,7 @@
import org.mule.runtime.module.artifact.activation.api.descriptor.DeployableArtifactDescriptorCreator;
import org.mule.runtime.module.artifact.activation.api.descriptor.DeployableArtifactDescriptorFactory;
import org.mule.runtime.module.artifact.activation.api.descriptor.DomainDescriptorResolutionException;
import org.mule.runtime.module.artifact.activation.api.descriptor.DomainDescriptorResolver;
import org.mule.runtime.module.artifact.activation.api.extension.discovery.ExtensionModelLoaderRepository;
import org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader;
import org.mule.runtime.module.artifact.api.classloader.ClassLoaderRepository;
Expand Down Expand Up @@ -160,14 +163,17 @@ public ApplicationDescriptor createArtifactDescriptor(File artifactLocation, Opt

public ApplicationDescriptor createArtifactDescriptor(File artifactLocation, DeployableProjectModel model,
Optional<Properties> deploymentProperties) {
return deployableArtifactDescriptorFactory
.createApplicationDescriptor(model, deploymentProperties.map(dp -> (Map<String, String>) fromProperties(dp))
.orElse(emptyMap()),
(domainName,
bundleDescriptor) -> getDomainForDescriptor(domainName, bundleDescriptor,
artifactLocation)
.getDescriptor(),
getDescriptorCreator());
String appName = artifactLocation.getName();
Optional<Properties> effectiveProperties =
deploymentProperties.isPresent() ? deploymentProperties : getPersistedDeploymentProperties(appName);
Map<String, String> properties = effectiveProperties
.map(dp -> (Map<String, String>) fromProperties(dp))
.orElse(emptyMap());
DomainDescriptorResolver domainDescriptorResolver =
(domainName, bundleDescriptor) -> getDomainForDescriptor(domainName, bundleDescriptor, artifactLocation).getDescriptor();

return deployableArtifactDescriptorFactory.createApplicationDescriptor(model, properties, domainDescriptorResolver,
getDescriptorCreator());
}

private DeployableArtifactDescriptorCreator<ApplicationDescriptor> getDescriptorCreator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import static org.mule.runtime.core.api.util.FileUtils.newFile;

import static java.io.File.separator;
import static java.util.Optional.empty;
import static java.util.Optional.of;

import org.mule.runtime.api.exception.MuleRuntimeException;
import org.mule.runtime.api.i18n.I18nMessage;
Expand All @@ -22,11 +24,16 @@
import java.util.Optional;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Common logic to create and persist a file containing deployment properties for each app/domain deployed/redeployed.
*/
public class DeploymentPropertiesUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(DeploymentPropertiesUtils.class);

private static final String DEPLOYMENT_PROPERTIES_FILE_NAME = "deployment.properties";

private static final String FLOWS_DEPLOYMENT_PROPERTIES_FILE_NAME = "flows.deployment.properties";
Expand All @@ -37,32 +44,70 @@ public class DeploymentPropertiesUtils {

/**
* This method resolves the deploymentProperties for a new deploy/redeploy considering the new deployment properties passed by
* the user as parameter and the deployment properties persisted in a previous deploy. In case no new deployment properties are
* passed, the previous persisted properties are returned. Otherwise, the new deployment properties are used and persisted in
* .mule/app/deployment-properties/<fileName>.
* the user as parameter and the deployment properties persisted in a previous deploy. The new deployment properties are used
* and persisted in .mule/app/deployment-properties/<fileName>.
*
* @param artifactName name of the artifact.
* @param deploymentProperties deployment properties set in the new deploy/redeploy as parameters.
* @param fileName name of the file where the deployment properties are persisted.
*
* @return deployment properties
* @throws IOException
*/
public static Properties resolveDeploymentProperties(String artifactName, Optional<Properties> deploymentProperties,
String fileName)
throws IOException {
File file = new File(getExecutionFolder(), artifactName);
String workingDirectory = file.getAbsolutePath();
String deploymentPropertiesPath = workingDirectory + separator + DEPLOYMENT_PROPERTIES_DIRECTORY;
Properties properties = deploymentProperties.orElse(new Properties());
setPersistedProperties(artifactName, properties, fileName);
return properties;
}

if (!deploymentProperties.isPresent()) {
return getDeploymentProperties(deploymentPropertiesPath, fileName);
public static Optional<Properties> getPersistedProperties(String artifactName, String fileName) {
try {
String deploymentPropertiesPath = getDeploymentPropertiesPath(artifactName);
return of(getDeploymentProperties(deploymentPropertiesPath, fileName));
} catch (IOException e) {
LOGGER.error("Failed to load persisted deployment property for artifact " + artifactName, e);
return empty();
}
}

public static Optional<Properties> getPersistedDeploymentProperties(String artifactName) {
return getPersistedProperties(artifactName, DEPLOYMENT_PROPERTIES_FILE_NAME);
}

public static Optional<Properties> getPersistedFlowDeploymentProperties(String artifactName) {
return getPersistedProperties(artifactName, FLOWS_DEPLOYMENT_PROPERTIES_FILE_NAME);
}

public static Optional<Properties> getPersistedArtifactStatusDeploymentProperties(String artifactName) {
return getPersistedProperties(artifactName, ARTIFACT_STATUS_DEPLOYMENT_PROPERTIES_FILE_NAME);
}

public static void setPersistedProperties(String artifactName, Properties properties, String fileName) throws IOException {
String deploymentPropertiesPath = getDeploymentPropertiesPath(artifactName);
initDeploymentPropertiesDirectory(deploymentPropertiesPath);
persistDeploymentPropertiesFile(deploymentPropertiesPath, deploymentProperties.get(), fileName);
persistDeploymentPropertiesFile(deploymentPropertiesPath, properties, fileName);
}

return deploymentProperties.get();
public static void setPersistedDeploymentProperties(String artifactName, Properties properties)
throws IOException {
setPersistedProperties(artifactName, properties, DEPLOYMENT_PROPERTIES_FILE_NAME);
}

public static void setPersistedFlowDeploymentProperties(String artifactName, Properties properties)
throws IOException {
setPersistedProperties(artifactName, properties, FLOWS_DEPLOYMENT_PROPERTIES_FILE_NAME);
}

public static void setPersistedArtifactStatusDeploymentProperties(String artifactName, Properties properties)
throws IOException {
setPersistedProperties(artifactName, properties, ARTIFACT_STATUS_DEPLOYMENT_PROPERTIES_FILE_NAME);
}

private static String getDeploymentPropertiesPath(String artifactName) {
File file = new File(getExecutionFolder(), artifactName);
String workingDirectory = file.getAbsolutePath();
return workingDirectory + separator + DEPLOYMENT_PROPERTIES_DIRECTORY;
}

/**
Expand All @@ -71,7 +116,6 @@ public static Properties resolveDeploymentProperties(String artifactName, Option
*
* @param deploymentPropertiesPath the path where the deployment properties are located
* @return deployment properties
*
* @throws IOException
*/
private static Properties getDeploymentProperties(String deploymentPropertiesPath, String fileName) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import static org.mule.runtime.core.api.config.MuleDeploymentProperties.MULE_LAZY_INIT_ENABLE_XML_VALIDATIONS_DEPLOYMENT_PROPERTY;
import static org.mule.runtime.core.internal.context.ArtifactStoppedPersistenceListener.ARTIFACT_STOPPED_LISTENER;
import static org.mule.runtime.module.deployment.impl.internal.artifact.ArtifactFactoryUtils.withArtifactMuleContext;
import static org.mule.runtime.module.deployment.impl.internal.util.DeploymentPropertiesUtils.getPersistedArtifactStatusDeploymentProperties;
import static org.mule.runtime.module.deployment.impl.internal.util.DeploymentPropertiesUtils.getPersistedFlowDeploymentProperties;
import static org.mule.runtime.module.deployment.impl.internal.util.DeploymentPropertiesUtils.resolveArtifactStatusDeploymentProperties;
import static org.mule.runtime.module.deployment.impl.internal.util.DeploymentPropertiesUtils.resolveFlowDeploymentProperties;
import static org.mule.runtime.module.deployment.internal.DefaultArchiveDeployer.START_ARTIFACT_ON_DEPLOYMENT_PROPERTY;
Expand Down Expand Up @@ -120,15 +122,9 @@ private void addFlowStoppedListeners(T artifact) {
}

public Boolean isStatePersisted(String flowName, String appName) {
Properties deploymentProperties = null;
try {
deploymentProperties = resolveFlowDeploymentProperties(appName, empty());
} catch (IOException e) {
logger.error("FlowStoppedDeploymentListener failed to process isStatePersisted for flow "
+ flowName, e);
}
return deploymentProperties != null
&& deploymentProperties.getProperty(flowName + "_" + START_FLOW_ON_DEPLOYMENT_PROPERTY) != null;
Optional<Properties> deploymentProperties = getPersistedFlowDeploymentProperties(appName);
return deploymentProperties.isPresent()
&& deploymentProperties.get().getProperty(flowName + "_" + START_FLOW_ON_DEPLOYMENT_PROPERTY) != null;
}

/**
Expand Down Expand Up @@ -212,15 +208,9 @@ private void tryToStopArtifact(T artifact) {
* deployment.
*/
private Boolean shouldStartArtifactAccordingToPersistedStatus(T artifact) {
Properties artifactStatusProperties = null;
try {
artifactStatusProperties = resolveArtifactStatusDeploymentProperties(artifact.getArtifactName(), empty());
} catch (IOException e) {
logger.error("Failed to load deployment property for artifact "
+ artifact.getArtifactName(), e);
}
return artifactStatusProperties != null
&& parseBoolean(artifactStatusProperties.getProperty(START_ARTIFACT_ON_DEPLOYMENT_PROPERTY, "true"));
Optional<Properties> artifactStatusProperties = getPersistedArtifactStatusDeploymentProperties(artifact.getArtifactName());
return artifactStatusProperties.isPresent()
&& parseBoolean(artifactStatusProperties.get().getProperty(START_ARTIFACT_ON_DEPLOYMENT_PROPERTY, "true"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.mule.runtime.api.util.MuleSystemProperties.DEPLOYMENT_APPLICATION_PROPERTY;
import static org.mule.runtime.container.api.MuleFoldersUtil.getDomainsFolder;
import static org.mule.runtime.core.internal.util.splash.SplashScreen.miniSplash;
import static org.mule.runtime.module.deployment.impl.internal.util.DeploymentPropertiesUtils.getPersistedDeploymentProperties;
import static org.mule.runtime.module.deployment.internal.DefaultArchiveDeployer.JAR_FILE_SUFFIX;
import static org.mule.runtime.module.deployment.internal.DefaultArchiveDeployer.ZIP_FILE_SUFFIX;
import static org.mule.runtime.module.deployment.internal.DeploymentUtils.deployExplodedDomains;
Expand All @@ -29,6 +30,7 @@
import static org.apache.commons.io.IOCase.INSENSITIVE;
import static org.apache.commons.io.filefilter.DirectoryFileFilter.DIRECTORY;
import static org.apache.commons.lang3.StringUtils.removeEnd;
import static org.apache.commons.lang3.StringUtils.removeEndIgnoreCase;
import static org.slf4j.LoggerFactory.getLogger;

import org.mule.runtime.api.scheduler.Scheduler;
Expand Down Expand Up @@ -57,6 +59,8 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
Expand Down Expand Up @@ -271,7 +275,9 @@ protected void deployPackedApps(String[] zips) {
for (String zip : zips) {
try {
// [SingleApp] Avoid filesystem polling and unify the watcher to directly invoke the deployment service.
applicationArchiveDeployer.deployPackagedArtifact(zip, empty());
final String artifactName = removeEndIgnoreCase(zip, JAR_FILE_SUFFIX);
Optional<Properties> deploymentProperties = getPersistedDeploymentProperties(artifactName);
applicationArchiveDeployer.deployPackagedArtifact(zip, deploymentProperties);
} catch (Exception e) {
// Ignore and continue
}
Expand All @@ -282,7 +288,8 @@ protected void deployExplodedApps(String[] apps) {
for (String addedApp : apps) {
try {
// [SingleApp] Avoid filesystem polling and unify the watcher to directly invoke the deployment service.
applicationArchiveDeployer.deployExplodedArtifact(addedApp, empty());
Optional<Properties> deploymentProperties = getPersistedDeploymentProperties(addedApp);
applicationArchiveDeployer.deployExplodedArtifact(addedApp, deploymentProperties);
} catch (DeploymentException e) {
// Ignore and continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
import static java.lang.String.valueOf;
import static java.util.Optional.empty;
import static java.util.Optional.of;

import static org.mule.runtime.module.deployment.impl.internal.util.DeploymentPropertiesUtils.getPersistedFlowDeploymentProperties;
import static org.mule.runtime.module.deployment.impl.internal.util.DeploymentPropertiesUtils.resolveFlowDeploymentProperties;
import static org.mule.runtime.module.deployment.impl.internal.util.DeploymentPropertiesUtils.setPersistedFlowDeploymentProperties;

import org.mule.runtime.core.internal.context.FlowStoppedPersistenceListener;

import java.io.IOException;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -47,10 +51,9 @@ public FlowStoppedDeploymentPersistenceListener(String flowName, String appName)
@Override
public void onStart() {
try {
Properties properties = resolveFlowDeploymentProperties(appName, empty());
Properties properties = getPersistedFlowDeploymentProperties(appName).orElse(new Properties());
properties.setProperty(propertyName, valueOf(true));

resolveFlowDeploymentProperties(appName, of(properties));
setPersistedFlowDeploymentProperties(appName, properties);
} catch (IOException e) {
logger.error("FlowStoppedDeploymentListener failed to process notification onStart for flow "
+ flowName, e);
Expand All @@ -63,10 +66,9 @@ public void onStop() {
return;
}
try {
Properties properties = resolveFlowDeploymentProperties(appName, empty());
Properties properties = getPersistedFlowDeploymentProperties(appName).orElse(new Properties());
properties.setProperty(propertyName, valueOf(false));

resolveFlowDeploymentProperties(appName, of(properties));
setPersistedFlowDeploymentProperties(appName, properties);
} catch (IOException e) {
logger.error("FlowStoppedDeploymentListener failed to process notification onStop for flow "
+ flowName, e);
Expand All @@ -80,14 +82,8 @@ public void doNotPersist() {

@Override
public Boolean shouldStart() {
Properties deploymentProperties = null;
try {
deploymentProperties = resolveFlowDeploymentProperties(appName, empty());
} catch (IOException e) {
logger.error("FlowStoppedDeploymentListener failed to process shouldStart for flow "
+ flowName, e);
}
return deploymentProperties != null
&& parseBoolean(deploymentProperties.getProperty(propertyName, "true"));
Optional<Properties> deploymentProperties = getPersistedFlowDeploymentProperties(appName);
return deploymentProperties.isPresent()
&& parseBoolean(deploymentProperties.get().getProperty(propertyName, "true"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.mule.runtime.api.util.MuleSystemProperties.SYSTEM_PROPERTY_PREFIX;
import static org.mule.runtime.container.api.MuleFoldersUtil.getAppsFolder;
import static org.mule.runtime.container.api.MuleFoldersUtil.getDomainsFolder;
import static org.mule.runtime.module.deployment.impl.internal.util.DeploymentPropertiesUtils.getPersistedDeploymentProperties;
import static org.mule.runtime.module.deployment.internal.ArtifactDeploymentTemplate.NOP_ARTIFACT_DEPLOYMENT_TEMPLATE;
import static org.mule.runtime.module.deployment.internal.DefaultArchiveDeployer.JAR_FILE_SUFFIX;
import static org.mule.runtime.module.deployment.internal.ParallelDeploymentDirectoryWatcher.MAX_APPS_IN_PARALLEL_DEPLOYMENT;
Expand Down Expand Up @@ -278,7 +279,7 @@ public void deploy(URI appArchiveUri, Properties appProperties) throws IOExcepti

@Override
public void redeploy(String artifactName) {
redeploy(artifactName, empty());
redeploy(artifactName, getPersistedDeploymentProperties(artifactName));
}

@Override
Expand Down Expand Up @@ -312,7 +313,8 @@ private void deployDomain(URI domainArchiveUri, Optional<Properties> deploymentP

@Override
public void redeployDomain(String domainName) {
redeployDomain(domainName, empty());
Optional<Properties> deploymentProperties = getPersistedDeploymentProperties(domainName);
redeployDomain(domainName, deploymentProperties);
}

private void redeployDomain(String domainName, Optional<Properties> deploymentProperties) {
Expand Down
Loading