Skip to content

Commit

Permalink
[WFCORE-6524] Do not duplicate managed deployment in content reposito…
Browse files Browse the repository at this point in the history
…ry in tmp/vfs/temp directory
  • Loading branch information
gaol committed Sep 30, 2023
1 parent 03213b9 commit faeface
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Closeable mountDeploymentContent(final VirtualFile contents, VirtualFile
assert contents != null : "null contents";
switch (type) {
case ZIP:
return VFS.mountZip(contents, mountPoint, tempFileProvider);
return VFS.mountZip(contents.getPhysicalFile(), mountPoint, tempFileProvider);
case EXPANDED:
return VFS.mountZipExpanded(contents, mountPoint, tempFileProvider);
case REAL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private synchronized ResourceRoot createResourceRoot(final VirtualFile file, fin
if(overlay != null) {
overlay.remountAsZip(false);
} else if(file.isFile()) {
closable = VFS.mountZip(file, file, TempFileProviderService.provider());
closable = VFS.mountZip(file.getPhysicalFile(), file, TempFileProviderService.provider());
}
final MountHandle mountHandle = MountHandle.create(closable);
final ResourceRoot resourceRoot = new ResourceRoot(file, mountHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ private static void parseResourceRoot(final DeploymentUnit deploymentUnit, final
if(overlay != null) {
overlay.remountAsZip(false);
} else if(child.isFile()) {
closable = VFS.mountZip(child, child, TempFileProviderService.provider());
closable = VFS.mountZip(child.getPhysicalFile(), child, TempFileProviderService.provider());
}
final MountHandle mountHandle = MountHandle.create(closable);
ResourceRoot resourceRoot = new ResourceRoot(name, child, mountHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ private static void parseResourceRoot(final DeploymentUnit deploymentUnit, final
if(overlay != null) {
overlay.remountAsZip(false);
} else if(child.isFile()) {
closable = VFS.mountZip(child, child, TempFileProviderService.provider());
closable = VFS.mountZip(child.getPhysicalFile(), child, TempFileProviderService.provider());
}
final MountHandle mountHandle = MountHandle.create(closable);
final ResourceRoot resourceRoot = new ResourceRoot(name, child, mountHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ private static void parseResourceRoot(final DeploymentUnit deploymentUnit, final
if(overlay != null) {
overlay.remountAsZip(false);
} else if(child.isFile()) {
closable = VFS.mountZip(child, child, TempFileProviderService.provider());
closable = VFS.mountZip(child.getPhysicalFile(), child, TempFileProviderService.provider());
}
final MountHandle mountHandle = MountHandle.create(closable);
final ResourceRoot resourceRoot = new ResourceRoot(name, child, mountHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ private static void parseResourceRoot(final DeploymentUnit deploymentUnit, final
if(overlay != null) {
overlay.remountAsZip(false);
} else if(child.isFile()) {
closable = VFS.mountZip(child, child, TempFileProviderService.provider());
closable = VFS.mountZip(child.getPhysicalFile(), child, TempFileProviderService.provider());
}
final MountHandle mountHandle = MountHandle.create(closable);
final ResourceRoot resourceRoot = new ResourceRoot(name, child, mountHandle);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.core.test.standalone.deployment;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import jakarta.inject.Inject;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.Operation;
import org.jboss.as.controller.operations.common.Util;
import org.jboss.as.repository.ContentFilter;
import org.jboss.as.repository.ContentRepositoryElement;
import org.jboss.as.repository.PathUtil;
import org.jboss.as.server.ServerEnvironment;
import org.jboss.as.test.deployment.trivial.ServiceActivatorDeployment;
import org.jboss.as.test.deployment.trivial.ServiceActivatorDeploymentUtil;
import org.jboss.as.test.integration.management.ManagementOperations;
import org.jboss.as.test.integration.management.util.ServerReload;
import org.jboss.dmr.ModelNode;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.core.testrunner.ManagementClient;
import org.wildfly.core.testrunner.WildFlyRunner;

/**
* Tests that there is no jar duplication on deployment to save disk space.
*
* @author <a href="mailto:aoingl@gmail.com">Lin Gao</a>
*/
@RunWith(WildFlyRunner.class)
public class DeploymentDuplicationTestCase {

private static final String DEPLOYMENT_NAME = DeploymentDuplicationTestCase.class.getSimpleName() + ".jar";

private static final Map<String, String> DEFAULT_MAP = Collections.singletonMap(ServiceActivatorDeployment.DEFAULT_SYS_PROP_NAME,
ServiceActivatorDeployment.DEFAULT_SYS_PROP_VALUE);

@Inject
private ManagementClient managementClient;

@Test
public void test() throws Exception {
ModelControllerClient mcc = managementClient.getControllerClient();
JavaArchive deployment = ServiceActivatorDeploymentUtil.createServiceActivatorDeploymentArchive(DEPLOYMENT_NAME, DEFAULT_MAP);
Operation deploymentOp = createDeploymentOp(deployment);
try {
ManagementOperations.executeOperation(mcc, deploymentOp);
ServiceActivatorDeploymentUtil.validateProperties(mcc, DEFAULT_MAP);

Path tempPath = Paths.get(ManagementOperations.executeOperation(mcc, readTempPathOp()).asString());
List<ContentRepositoryElement> elements = PathUtil.listFiles(tempPath, null, new ContentFilter() {
@Override
public boolean acceptFile(Path rootPath, Path file) throws IOException {
return file.endsWith("content");
}
@Override
public boolean acceptFile(Path rootPath, Path file, InputStream in) throws IOException {
return file.endsWith("content");
}
@Override
public boolean acceptDirectory(Path rootPath, Path path) throws IOException {
return false;
}
});
// Check temp directory to make sure no duplication
Assert.assertTrue("There should be no content file in the tmp directory", elements.isEmpty());
} finally {
ManagementOperations.executeOperation(mcc, Util.createRemoveOperation(PathAddress.pathAddress("deployment", deployment.getName())));
ServerReload.executeReloadAndWaitForCompletion(mcc);
}
}

private Operation readTempPathOp() {
final ModelNode readAttributeOperation = Util.getReadAttributeOperation(PathAddress.pathAddress("path", ServerEnvironment.SERVER_TEMP_DIR), "path");
return Operation.Factory.create(readAttributeOperation, Collections.emptyList(), true);
}

private Operation createDeploymentOp(JavaArchive deployment) {
final List<InputStream> streams = new ArrayList<>();
streams.add(deployment.as(ZipExporter.class).exportAsInputStream());
final ModelNode addOperation = Util.createAddOperation(PathAddress.pathAddress("deployment", deployment.getName()));
addOperation.get("enabled").set(true);
addOperation.get("content").add().get("input-stream-index").set(0);
return Operation.Factory.create(addOperation, streams, true);
}

}

0 comments on commit faeface

Please sign in to comment.