|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.apache.hadoop.yarn.submarine.runtimes.yarnservice; |
| 18 | + |
| 19 | +import com.google.common.annotations.VisibleForTesting; |
| 20 | +import org.apache.hadoop.conf.Configuration; |
| 21 | +import org.apache.hadoop.fs.FileStatus; |
| 22 | +import org.apache.hadoop.fs.FileSystem; |
| 23 | +import org.apache.hadoop.fs.FileUtil; |
| 24 | +import org.apache.hadoop.fs.Path; |
| 25 | +import org.apache.hadoop.fs.permission.FsPermission; |
| 26 | +import org.apache.hadoop.yarn.service.api.records.Component; |
| 27 | +import org.apache.hadoop.yarn.service.api.records.ConfigFile; |
| 28 | +import org.apache.hadoop.yarn.submarine.common.ClientContext; |
| 29 | +import org.apache.hadoop.yarn.submarine.common.conf.SubmarineConfiguration; |
| 30 | +import org.apache.hadoop.yarn.submarine.common.conf.SubmarineLogs; |
| 31 | +import org.apache.hadoop.yarn.submarine.common.fs.RemoteDirectoryManager; |
| 32 | +import org.apache.hadoop.yarn.submarine.utils.ZipUtilities; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +import java.io.File; |
| 37 | +import java.io.FileNotFoundException; |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.HashSet; |
| 40 | +import java.util.Set; |
| 41 | + |
| 42 | +/** |
| 43 | + * Contains methods to perform file system operations. Almost all of the methods |
| 44 | + * are regular non-static methods as the operations are performed with the help |
| 45 | + * of a {@link RemoteDirectoryManager} instance passed in as a constructor |
| 46 | + * dependency. Please note that some operations require to read config settings |
| 47 | + * as well, so that we have Submarine and YARN config objects as dependencies as |
| 48 | + * well. |
| 49 | + */ |
| 50 | +public class FileSystemOperations { |
| 51 | + private static final Logger LOG = |
| 52 | + LoggerFactory.getLogger(FileSystemOperations.class); |
| 53 | + private final Configuration submarineConfig; |
| 54 | + private final Configuration yarnConfig; |
| 55 | + |
| 56 | + private Set<Path> uploadedFiles = new HashSet<>(); |
| 57 | + private RemoteDirectoryManager remoteDirectoryManager; |
| 58 | + |
| 59 | + public FileSystemOperations(ClientContext clientContext) { |
| 60 | + this.remoteDirectoryManager = clientContext.getRemoteDirectoryManager(); |
| 61 | + this.submarineConfig = clientContext.getSubmarineConfig(); |
| 62 | + this.yarnConfig = clientContext.getYarnConfig(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * May download a remote uri(file/dir) and zip. |
| 67 | + * Skip download if local dir |
| 68 | + * Remote uri can be a local dir(won't download) |
| 69 | + * or remote HDFS dir, s3 dir/file .etc |
| 70 | + * */ |
| 71 | + public String downloadAndZip(String remoteDir, String zipFileName, |
| 72 | + boolean doZip) |
| 73 | + throws IOException { |
| 74 | + //Append original modification time and size to zip file name |
| 75 | + String suffix; |
| 76 | + String srcDir = remoteDir; |
| 77 | + String zipDirPath = |
| 78 | + System.getProperty("java.io.tmpdir") + "/" + zipFileName; |
| 79 | + boolean needDeleteTempDir = false; |
| 80 | + if (remoteDirectoryManager.isRemote(remoteDir)) { |
| 81 | + //Append original modification time and size to zip file name |
| 82 | + FileStatus status = |
| 83 | + remoteDirectoryManager.getRemoteFileStatus(new Path(remoteDir)); |
| 84 | + suffix = "_" + status.getModificationTime() |
| 85 | + + "-" + remoteDirectoryManager.getRemoteFileSize(remoteDir); |
| 86 | + // Download them to temp dir |
| 87 | + boolean downloaded = |
| 88 | + remoteDirectoryManager.copyRemoteToLocal(remoteDir, zipDirPath); |
| 89 | + if (!downloaded) { |
| 90 | + throw new IOException("Failed to download files from " |
| 91 | + + remoteDir); |
| 92 | + } |
| 93 | + LOG.info("Downloaded remote: {} to local: {}", remoteDir, zipDirPath); |
| 94 | + srcDir = zipDirPath; |
| 95 | + needDeleteTempDir = true; |
| 96 | + } else { |
| 97 | + File localDir = new File(remoteDir); |
| 98 | + suffix = "_" + localDir.lastModified() |
| 99 | + + "-" + localDir.length(); |
| 100 | + } |
| 101 | + if (!doZip) { |
| 102 | + return srcDir; |
| 103 | + } |
| 104 | + // zip a local dir |
| 105 | + String zipFileUri = |
| 106 | + ZipUtilities.zipDir(srcDir, zipDirPath + suffix + ".zip"); |
| 107 | + // delete downloaded temp dir |
| 108 | + if (needDeleteTempDir) { |
| 109 | + deleteFiles(srcDir); |
| 110 | + } |
| 111 | + return zipFileUri; |
| 112 | + } |
| 113 | + |
| 114 | + public void deleteFiles(String localUri) { |
| 115 | + boolean success = FileUtil.fullyDelete(new File(localUri)); |
| 116 | + if (!success) { |
| 117 | + LOG.warn("Failed to delete {}", localUri); |
| 118 | + } |
| 119 | + LOG.info("Deleted {}", localUri); |
| 120 | + } |
| 121 | + |
| 122 | + @VisibleForTesting |
| 123 | + public void uploadToRemoteFileAndLocalizeToContainerWorkDir(Path stagingDir, |
| 124 | + String fileToUpload, String destFilename, Component comp) |
| 125 | + throws IOException { |
| 126 | + Path uploadedFilePath = uploadToRemoteFile(stagingDir, fileToUpload); |
| 127 | + locateRemoteFileToContainerWorkDir(destFilename, comp, uploadedFilePath); |
| 128 | + } |
| 129 | + |
| 130 | + private void locateRemoteFileToContainerWorkDir(String destFilename, |
| 131 | + Component comp, Path uploadedFilePath) |
| 132 | + throws IOException { |
| 133 | + FileSystem fs = FileSystem.get(yarnConfig); |
| 134 | + |
| 135 | + FileStatus fileStatus = fs.getFileStatus(uploadedFilePath); |
| 136 | + LOG.info("Uploaded file path = " + fileStatus.getPath()); |
| 137 | + |
| 138 | + // Set it to component's files list |
| 139 | + comp.getConfiguration().getFiles().add(new ConfigFile().srcFile( |
| 140 | + fileStatus.getPath().toUri().toString()).destFile(destFilename) |
| 141 | + .type(ConfigFile.TypeEnum.STATIC)); |
| 142 | + } |
| 143 | + |
| 144 | + public Path uploadToRemoteFile(Path stagingDir, String fileToUpload) throws |
| 145 | + IOException { |
| 146 | + FileSystem fs = remoteDirectoryManager.getDefaultFileSystem(); |
| 147 | + |
| 148 | + // Upload to remote FS under staging area |
| 149 | + File localFile = new File(fileToUpload); |
| 150 | + if (!localFile.exists()) { |
| 151 | + throw new FileNotFoundException( |
| 152 | + "Trying to upload file=" + localFile.getAbsolutePath() |
| 153 | + + " to remote, but couldn't find local file."); |
| 154 | + } |
| 155 | + String filename = new File(fileToUpload).getName(); |
| 156 | + |
| 157 | + Path uploadedFilePath = new Path(stagingDir, filename); |
| 158 | + if (!uploadedFiles.contains(uploadedFilePath)) { |
| 159 | + if (SubmarineLogs.isVerbose()) { |
| 160 | + LOG.info("Copying local file=" + fileToUpload + " to remote=" |
| 161 | + + uploadedFilePath); |
| 162 | + } |
| 163 | + fs.copyFromLocalFile(new Path(fileToUpload), uploadedFilePath); |
| 164 | + uploadedFiles.add(uploadedFilePath); |
| 165 | + } |
| 166 | + return uploadedFilePath; |
| 167 | + } |
| 168 | + |
| 169 | + public void validFileSize(String uri) throws IOException { |
| 170 | + long actualSizeByte; |
| 171 | + String locationType = "Local"; |
| 172 | + if (remoteDirectoryManager.isRemote(uri)) { |
| 173 | + actualSizeByte = remoteDirectoryManager.getRemoteFileSize(uri); |
| 174 | + locationType = "Remote"; |
| 175 | + } else { |
| 176 | + actualSizeByte = FileUtil.getDU(new File(uri)); |
| 177 | + } |
| 178 | + long maxFileSizeMB = submarineConfig |
| 179 | + .getLong(SubmarineConfiguration.LOCALIZATION_MAX_ALLOWED_FILE_SIZE_MB, |
| 180 | + SubmarineConfiguration.DEFAULT_MAX_ALLOWED_REMOTE_URI_SIZE_MB); |
| 181 | + LOG.info("{} fie/dir: {}, size(Byte):{}," |
| 182 | + + " Allowed max file/dir size: {}", |
| 183 | + locationType, uri, actualSizeByte, maxFileSizeMB * 1024 * 1024); |
| 184 | + |
| 185 | + if (actualSizeByte > maxFileSizeMB * 1024 * 1024) { |
| 186 | + throw new IOException(uri + " size(Byte): " |
| 187 | + + actualSizeByte + " exceeds configured max size:" |
| 188 | + + maxFileSizeMB * 1024 * 1024); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + public void setPermission(Path destPath, FsPermission permission) throws |
| 193 | + IOException { |
| 194 | + FileSystem fs = FileSystem.get(yarnConfig); |
| 195 | + fs.setPermission(destPath, new FsPermission(permission)); |
| 196 | + } |
| 197 | + |
| 198 | + public static boolean needHdfs(String content) { |
| 199 | + return content != null && content.contains("hdfs://"); |
| 200 | + } |
| 201 | +} |
0 commit comments