valepakh commented on code in PR #6625: URL: https://github.com/apache/ignite-3/pull/6625#discussion_r2376329927
########## modules/code-deployment/src/main/java/org/apache/ignite/internal/deployunit/DeployerProcessor.java: ########## @@ -0,0 +1,89 @@ +/* + * 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> + * </ul> + */ +public class DeployerProcessor implements DeploymentUnitProcessor<Path> { + /** Suffix used for temporary files during the deployment process. */ + private static final String TMP_SUFFIX = ".tmp"; + + @Override + public void processContent(DeploymentUnitImpl unit, Path unitFolder) throws IOException { + for (Entry<String, InputStream> e : unit.content().entrySet()) { + doDeploy(unitFolder, e.getKey(), e.getValue()); + } + } + + @Override + public void processContentWithUnzip(ZipDeploymentUnit unit, Path unitFolder) throws IOException { + ZipInputStream zis = unit.zis(); + Path zipUnitFolder = unitFolder; Review Comment: ```suggestion ``` ########## modules/code-deployment/src/main/java/org/apache/ignite/internal/deployunit/DeploymentUnitImpl.java: ########## @@ -0,0 +1,67 @@ +/* + * 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 org.apache.ignite.internal.util.IgniteUtils.closeAll; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; + +/** + * Standard implementation of {@link DeploymentUnit} that handles regular (non-compressed) deployment content. + * + * <p>This class represents a deployment unit containing a collection of files, where each file is + * represented as a mapping from file name to its corresponding {@link InputStream}. This implementation is designed for straightforward + * deployment scenarios where the content does not require compression or special extraction handling. + * + * <p>The {@code DeploymentUnitImpl} is the primary implementation used for most deployment operations + * in the Apache Ignite code deployment system. It provides a simple and efficient way to package and deploy code artifacts, configuration + * files, resources, and other deployment assets to Ignite cluster nodes. + */ +public class DeploymentUnitImpl implements DeploymentUnit { Review Comment: Let's rename it, maybe `FilesDeploymentUnit`? ########## modules/rest/src/main/java/org/apache/ignite/internal/rest/deployment/ZipInputStreamCollector.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.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.Objects; +import java.util.zip.ZipInputStream; +import org.apache.ignite.internal.deployunit.DeploymentUnit; +import org.apache.ignite.internal.deployunit.ZipDeploymentUnit; + +/** + * 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 { + private static final byte[] ZIP_MAGIC_HEADER = {0x50, 0x4b, 0x03, 0x04}; + + /** + * 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; + } + + @Override + public void addInputStream(String filename, InputStream is) { + InputStream result = is.markSupported() ? is : new BufferedInputStream(is); + + if (isZip(result)) { Review Comment: Should we throw an exception if input stream is not a zip? Can you add a test for the controller? ########## modules/code-deployment/src/main/java/org/apache/ignite/internal/deployunit/DeployerProcessor.java: ########## @@ -0,0 +1,89 @@ +/* + * 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> + * </ul> + */ +public class DeployerProcessor implements DeploymentUnitProcessor<Path> { + /** Suffix used for temporary files during the deployment process. */ + private static final String TMP_SUFFIX = ".tmp"; + + @Override + public void processContent(DeploymentUnitImpl unit, Path unitFolder) throws IOException { + for (Entry<String, InputStream> e : unit.content().entrySet()) { + doDeploy(unitFolder, e.getKey(), e.getValue()); + } + } + + @Override + public void processContentWithUnzip(ZipDeploymentUnit unit, Path unitFolder) throws IOException { + ZipInputStream zis = unit.zis(); + Path zipUnitFolder = unitFolder; + ZipEntry ze; + while ((ze = zis.getNextEntry()) != null) { + Path entryPath = zipUnitFolder.resolve(ze.getName()); Review Comment: ```suggestion Path entryPath = unitFolder.resolve(ze.getName()); ``` ########## modules/code-deployment/src/main/java/org/apache/ignite/internal/deployunit/DeployerProcessor.java: ########## @@ -0,0 +1,89 @@ +/* + * 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> + * </ul> + */ +public class DeployerProcessor implements DeploymentUnitProcessor<Path> { + /** Suffix used for temporary files during the deployment process. */ + private static final String TMP_SUFFIX = ".tmp"; + + @Override + public void processContent(DeploymentUnitImpl unit, Path unitFolder) throws IOException { + for (Entry<String, InputStream> e : unit.content().entrySet()) { + doDeploy(unitFolder, e.getKey(), e.getValue()); + } + } + + @Override + public void processContentWithUnzip(ZipDeploymentUnit unit, Path unitFolder) throws IOException { + ZipInputStream zis = unit.zis(); + Path zipUnitFolder = unitFolder; + ZipEntry ze; + while ((ze = zis.getNextEntry()) != null) { + Path entryPath = zipUnitFolder.resolve(ze.getName()); + + if (ze.isDirectory()) { + Files.createDirectories(entryPath); + } else { + Path unitFileFolder = entryPath.getParent(); + Files.createDirectories(unitFileFolder); Review Comment: Is `createDirectories` needed here? `doDeploy` method will create all parent directories for the actual files. Also why we need to resolve the `entryPath` at all? We resolve it, get its filename, then resolve it again against its parent in the `doDeploy` ########## modules/code-deployment/src/main/java/org/apache/ignite/internal/deployunit/DeploymentUnitImpl.java: ########## @@ -0,0 +1,67 @@ +/* + * 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 org.apache.ignite.internal.util.IgniteUtils.closeAll; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; + +/** + * Standard implementation of {@link DeploymentUnit} that handles regular (non-compressed) deployment content. + * + * <p>This class represents a deployment unit containing a collection of files, where each file is + * represented as a mapping from file name to its corresponding {@link InputStream}. This implementation is designed for straightforward + * deployment scenarios where the content does not require compression or special extraction handling. + * + * <p>The {@code DeploymentUnitImpl} is the primary implementation used for most deployment operations + * in the Apache Ignite code deployment system. It provides a simple and efficient way to package and deploy code artifacts, configuration + * files, resources, and other deployment assets to Ignite cluster nodes. + */ +public class DeploymentUnitImpl implements DeploymentUnit { + /** + * The deployment unit content represented as a mapping from file names to their input streams. Each entry represents a file within the + * deployment unit. + */ + private final Map<String, InputStream> content; + + /** + * Constructor. + */ + public DeploymentUnitImpl(Map<String, InputStream> content) { + this.content = content; + } + + /** + * Returns the deployment unit content as a map of file names to input streams. + */ + public Map<String, InputStream> content() { + return content; + } + + @Override + public void close() throws Exception { + closeAll(content.values()); + } + + @Override + public <T> void process(DeploymentUnitProcessor<T> processor, T arg) throws IOException { + processor.processContent(this, arg); Review Comment: Can we pass content directly to the `processContent` method? I don't see any value in passing the implementation class. Same for the `ZipDeploymentUnit` ########## modules/rest/src/main/java/org/apache/ignite/internal/rest/deployment/InputStreamCollectorImpl.java: ########## @@ -0,0 +1,60 @@ +/* + * 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.InputStream; +import java.util.HashMap; +import java.util.Map; +import org.apache.ignite.internal.deployunit.DeploymentUnit; +import org.apache.ignite.internal.deployunit.DeploymentUnitImpl; + +/** + * Standard implementation of {@link InputStreamCollector} for collecting regular file content. + * + * <p>This implementation provides a straightforward approach to collecting input streams and + * converting them into a standard {@link DeploymentUnitImpl}. It maintains an internal map + * of filename-to-stream associations and creates deployment units containing regular + * (non-compressed) file content. + */ +public class InputStreamCollectorImpl implements InputStreamCollector { + /** + * Internal storage for collected input streams mapped by their filenames. + * The map maintains the association between logical file paths and their content streams. + */ + private final Map<String, InputStream> content = new HashMap<>(); + + /** {@inheritDoc} */ Review Comment: Please remove these -- 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]
