Copilot commented on code in PR #6625: URL: https://github.com/apache/ignite-3/pull/6625#discussion_r2362267453
########## modules/rest/src/main/java/org/apache/ignite/internal/rest/deployment/ZipInputStreamCollector.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.ignite.internal.rest.deployment; + +import static org.apache.ignite.internal.util.IgniteUtils.closeAll; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipInputStream; +import org.apache.ignite.internal.deployunit.DeploymentUnit; +import org.apache.ignite.internal.deployunit.ZipDeploymentUnit; +import org.jetbrains.annotations.Nullable; + +/** + * Advanced implementation of {@link InputStreamCollector} that automatically detects and handles ZIP content. + * + * <p>This decorator implementation wraps another {@link InputStreamCollector} and provides intelligent + * handling of mixed content types. It automatically detects ZIP archives among the added input streams + * and separates them from regular file content, creating a {@link ZipDeploymentUnit} that can handle + * both types of content appropriately. + */ +public class ZipInputStreamCollector implements InputStreamCollector { + /** + * The delegate collector that handles regular (non-ZIP) content. + * All streams that are not identified as ZIP archives are forwarded to this collector. + */ + private final InputStreamCollector delegate; + + /** + * Collection of detected ZIP input streams that require special processing. + * These streams will be handled by the ZipDeploymentUnit for automatic extraction. + */ + private final List<ZipInputStream> zipContent = new ArrayList<>(); + + /** + * Constructor. + */ + public ZipInputStreamCollector(InputStreamCollector delegate) { + this.delegate = delegate; + } + + /** {@inheritDoc} */ + @Override + public void addInputStream(String filename, InputStream is) { + InputStream result = is.markSupported() ? is : new BufferedInputStream(is); + + ZipInputStream zis = tryZip(result); + if (zis != null) { + zipContent.add(zis); + } else { + delegate.addInputStream(filename, result); + } + } + + /** {@inheritDoc} */ + @Nullable + private static ZipInputStream tryZip(InputStream is) { + try { + ZipInputStream zis = new ZipInputStream(is); + if (zis.getNextEntry() != null) { + is.reset(); + return zis; + } else { + is.reset(); Review Comment: When no ZIP entry is found, the method resets the InputStream but doesn't close the ZipInputStream created on line 77. This creates a resource leak. The ZipInputStream should be closed before returning null. ```suggestion is.reset(); zis.close(); ``` ########## modules/rest/src/main/java/org/apache/ignite/internal/rest/deployment/ZipInputStreamCollector.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.ignite.internal.rest.deployment; + +import static org.apache.ignite.internal.util.IgniteUtils.closeAll; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipInputStream; +import org.apache.ignite.internal.deployunit.DeploymentUnit; +import org.apache.ignite.internal.deployunit.ZipDeploymentUnit; +import org.jetbrains.annotations.Nullable; + +/** + * Advanced implementation of {@link InputStreamCollector} that automatically detects and handles ZIP content. + * + * <p>This decorator implementation wraps another {@link InputStreamCollector} and provides intelligent + * handling of mixed content types. It automatically detects ZIP archives among the added input streams + * and separates them from regular file content, creating a {@link ZipDeploymentUnit} that can handle + * both types of content appropriately. + */ +public class ZipInputStreamCollector implements InputStreamCollector { + /** + * The delegate collector that handles regular (non-ZIP) content. + * All streams that are not identified as ZIP archives are forwarded to this collector. + */ + private final InputStreamCollector delegate; + + /** + * Collection of detected ZIP input streams that require special processing. + * These streams will be handled by the ZipDeploymentUnit for automatic extraction. + */ + private final List<ZipInputStream> zipContent = new ArrayList<>(); + + /** + * Constructor. + */ + public ZipInputStreamCollector(InputStreamCollector delegate) { + this.delegate = delegate; + } + + /** {@inheritDoc} */ + @Override + public void addInputStream(String filename, InputStream is) { + InputStream result = is.markSupported() ? is : new BufferedInputStream(is); + + ZipInputStream zis = tryZip(result); + if (zis != null) { + zipContent.add(zis); + } else { + delegate.addInputStream(filename, result); + } + } + + /** {@inheritDoc} */ + @Nullable + private static ZipInputStream tryZip(InputStream is) { + try { + ZipInputStream zis = new ZipInputStream(is); + if (zis.getNextEntry() != null) { + is.reset(); + return zis; + } else { + is.reset(); + return null; + } + } catch (IOException e) { + return null; + } + } Review Comment: The ZipInputStream created on line 77 is returned on line 80 without resetting the underlying InputStream. This will cause the returned ZipInputStream to be positioned incorrectly. The InputStream should be reset before creating a new ZipInputStream to return. ########## modules/code-deployment/src/main/java/org/apache/ignite/internal/deployunit/DeployDeploymentUnitProcessor.java: ########## @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.ignite.internal.deployunit; + +import static java.nio.file.StandardCopyOption.ATOMIC_MOVE; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map.Entry; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +/** + * Implementation of {@link DeploymentUnitProcessor} that deploys deployment unit content to the file system. + * + * <p>This processor extracts and deploys files from deployment units to a specified target directory. + * It handles both regular deployment units (containing individual files as input streams) and ZIP-based + * deployment units (containing compressed archives that need extraction). + * + * <p>The deployment process ensures atomic file operations by: + * <ul> + * <li>First copying files to temporary locations with a {@code .tmp} suffix</li> + * <li>Then atomically moving them to their final destinations</li> + * <li>Creating necessary parent directories as needed</li> + * </ul> + * + * <p>This approach prevents partial deployments and ensures that files are either fully deployed + * or not deployed at all, maintaining consistency during the deployment process. + * + * <p>Type parameters: + * <ul> + * <li>{@code Path} - the argument type representing the target deployment directory</li> + * <li>{@code Void} - the return type (no meaningful return value)</li> + * </ul> + */ +public class DeployDeploymentUnitProcessor implements DeploymentUnitProcessor<Path, Void> { + /** Suffix used for temporary files during the deployment process. */ + private static final String TMP_SUFFIX = ".tmp"; + + /** {@inheritDoc} */ + @Override + public Void processContent(DeploymentUnitImpl unit, Path unitFolder) throws IOException { + for (Entry<String, InputStream> e : unit.content().entrySet()) { + doDeploy(unitFolder, e.getKey(), e.getValue()); + } + return null; + } + + /** {@inheritDoc} */ + @Override + public Void processContentWithUnzip(ZipDeploymentUnit unit, Path unitFolder) throws IOException { + for (ZipInputStream zis : unit.zipContent()) { + ZipEntry ze; + while ((ze = zis.getNextEntry()) != null) { + Path entryPath = unitFolder.resolve(ze.getName()); + + if (ze.isDirectory()) { + Files.createDirectories(entryPath); + } else { + Path unitFileFolder = entryPath.getParent(); + Files.createDirectories(unitFileFolder); + doDeploy(unitFileFolder, entryPath.getFileName().toString(), zis); Review Comment: The doDeploy method is called with unitFileFolder (parent directory) but should be called with the directory where the file should be created. The call should use unitFolder instead of unitFileFolder to maintain consistency with the processContent method. ```suggestion doDeploy(unitFolder, ze.getName(), zis); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
