Search in sources :

Example 11 with CompactFileState

use of org.smartdata.model.CompactFileState in project SSM by Intel-bigdata.

the class SmartDFSClient method getLocatedBlocks.

@Override
public LocatedBlocks getLocatedBlocks(String src, long start) throws IOException {
    LocatedBlocks locatedBlocks = super.getLocatedBlocks(src, start);
    if (!CALLER_CLASS.equals(Thread.currentThread().getStackTrace()[2].getClassName()) && locatedBlocks.getFileLength() == 0) {
        FileState fileState = getFileState(src);
        if (fileState instanceof CompactFileState) {
            String containerFile = ((CompactFileState) fileState).getFileContainerInfo().getContainerFilePath();
            long offset = ((CompactFileState) fileState).getFileContainerInfo().getOffset();
            return super.getLocatedBlocks(containerFile, offset + start);
        }
    }
    return locatedBlocks;
}
Also used : NormalFileState(org.smartdata.model.NormalFileState) CompactFileState(org.smartdata.model.CompactFileState) FileState(org.smartdata.model.FileState) CompressionFileState(org.smartdata.model.CompressionFileState) LocatedBlocks(org.apache.hadoop.hdfs.protocol.LocatedBlocks) CompactFileState(org.smartdata.model.CompactFileState)

Example 12 with CompactFileState

use of org.smartdata.model.CompactFileState in project SSM by Intel-bigdata.

the class SmallFileCompactAction method execute.

@Override
protected void execute() throws Exception {
    // Set hdfs client by DFSClient rather than SmartDFSClient
    this.setDfsClient(HadoopUtil.getDFSClient(HadoopUtil.getNameNodeUri(conf), conf));
    // Get small file list
    if (smallFiles == null || smallFiles.isEmpty()) {
        throw new IllegalArgumentException(String.format("Invalid small files: %s.", smallFiles));
    }
    ArrayList<String> smallFileList = new Gson().fromJson(smallFiles, new TypeToken<ArrayList<String>>() {
    }.getType());
    if (smallFileList == null || smallFileList.isEmpty()) {
        throw new IllegalArgumentException(String.format("Invalid small files: %s.", smallFiles));
    }
    // Get container file path
    if (containerFile == null || containerFile.isEmpty()) {
        throw new IllegalArgumentException(String.format("Invalid container file: %s.", containerFile));
    }
    // Get container file permission
    SmartFilePermission filePermission = null;
    if (containerFilePermission != null && !containerFilePermission.isEmpty()) {
        filePermission = new Gson().fromJson(containerFilePermission, new TypeToken<SmartFilePermission>() {
        }.getType());
    }
    appendLog(String.format("Action starts at %s : compact small files to %s.", Utils.getFormatedCurrentTime(), containerFile));
    // Get initial offset and output stream
    // Create container file and set permission if not exists
    long offset;
    OutputStream out;
    boolean isContainerFileExist = dfsClient.exists(containerFile);
    if (isContainerFileExist) {
        offset = dfsClient.getFileInfo(containerFile).getLen();
        out = CompatibilityHelperLoader.getHelper().getDFSClientAppend(dfsClient, containerFile, 64 * 1024, offset);
    } else {
        out = dfsClient.create(containerFile, true);
        if (filePermission != null) {
            dfsClient.setOwner(containerFile, filePermission.getOwner(), filePermission.getGroup());
            dfsClient.setPermission(containerFile, new FsPermission(filePermission.getPermission()));
        }
        offset = 0L;
    }
    List<CompactFileState> compactFileStates = new ArrayList<>();
    for (String smallFile : smallFileList) {
        if ((smallFile != null) && !smallFile.isEmpty() && dfsClient.exists(smallFile)) {
            HdfsDataOutputStream append = (HdfsDataOutputStream) CompatibilityHelperLoader.getHelper().getDFSClientAppend(dfsClient, smallFile, 1024);
            long fileLen = dfsClient.getFileInfo(smallFile).getLen();
            if (fileLen > 0) {
                try (InputStream in = dfsClient.open(smallFile)) {
                    // Copy bytes of small file to container file
                    IOUtils.copyBytes(in, out, 4096);
                    // Truncate small file, add file container info to XAttr
                    CompactFileState compactFileState = new CompactFileState(smallFile, new FileContainerInfo(containerFile, offset, fileLen));
                    append.close();
                    truncateAndSetXAttr(smallFile, compactFileState);
                    // Update compact file state map, offset, status, and log
                    compactFileStates.add(compactFileState);
                    offset += fileLen;
                    this.status = (smallFileList.indexOf(smallFile) + 1.0f) / smallFileList.size();
                    appendLog(String.format("Compact %s to %s successfully.", smallFile, containerFile));
                } catch (IOException e) {
                    // Close append, output streams and put compact file state map into action result
                    if (append != null) {
                        append.close();
                    }
                    if (out != null) {
                        out.close();
                        appendResult(new Gson().toJson(compactFileStates));
                    }
                    if (!isContainerFileExist && compactFileStates.isEmpty()) {
                        dfsClient.delete(containerFile, false);
                    }
                    throw e;
                }
            }
        }
    }
    appendResult(new Gson().toJson(compactFileStates));
    if (out != null) {
        out.close();
    }
    if (!isContainerFileExist && compactFileStates.isEmpty()) {
        dfsClient.delete(containerFile, false);
    }
    appendLog(String.format("Compact all the small files to %s successfully.", containerFile));
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HdfsDataOutputStream(org.apache.hadoop.hdfs.client.HdfsDataOutputStream) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) SmartFilePermission(org.smartdata.SmartFilePermission) IOException(java.io.IOException) TypeToken(com.google.gson.reflect.TypeToken) HdfsDataOutputStream(org.apache.hadoop.hdfs.client.HdfsDataOutputStream) FsPermission(org.apache.hadoop.fs.permission.FsPermission) FileContainerInfo(org.smartdata.model.FileContainerInfo) CompactFileState(org.smartdata.model.CompactFileState)

Example 13 with CompactFileState

use of org.smartdata.model.CompactFileState in project SSM by Intel-bigdata.

the class SmallFileScheduler method handleCompactActionResult.

/**
 * Handle compact action result.
 */
private void handleCompactActionResult(ActionInfo actionInfo) {
    // Get container file path, small files, result of this action
    String containerFilePath = getContainerFile(actionInfo);
    List<String> smallFileList = new Gson().fromJson(actionInfo.getArgs().get(HdfsAction.FILE_PATH), new TypeToken<ArrayList<String>>() {
    }.getType());
    List<CompactFileState> compactFileStates = new Gson().fromJson(actionInfo.getResult(), new TypeToken<ArrayList<CompactFileState>>() {
    }.getType());
    // handling small file cache
    if (compactFileStates != null && !compactFileStates.isEmpty()) {
        LOG.debug(String.format("Add container file %s into cache.", containerFilePath));
        containerFileCache.add(containerFilePath);
        for (CompactFileState compactFileState : compactFileStates) {
            handlingSmallFileCache.add(compactFileState.getPath());
            compactFileStateQueue.offer(compactFileState);
        }
    }
    // Remove locks of container file and small files
    containerFileLock.remove(containerFilePath);
    compactSmallFileLock.removeAll(smallFileList);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) CompactFileState(org.smartdata.model.CompactFileState)

Example 14 with CompactFileState

use of org.smartdata.model.CompactFileState in project SSM by Intel-bigdata.

the class SmartFileSystem method isFileClosed.

@Override
public boolean isFileClosed(final Path src) throws IOException {
    boolean isFileClosed = super.isFileClosed(src);
    if (!isFileClosed) {
        FileState fileState = smartDFSClient.getFileState(getPathName(src));
        if (fileState instanceof CompactFileState) {
            String containerFile = ((CompactFileState) fileState).getFileContainerInfo().getContainerFilePath();
            isFileClosed = smartDFSClient.isFileClosed(containerFile);
        }
    }
    return isFileClosed;
}
Also used : CompactFileState(org.smartdata.model.CompactFileState) FileState(org.smartdata.model.FileState) CompressionFileState(org.smartdata.model.CompressionFileState) CompactFileState(org.smartdata.model.CompactFileState)

Example 15 with CompactFileState

use of org.smartdata.model.CompactFileState in project SSM by Intel-bigdata.

the class SmartDFSClient method getFileChecksum.

@Override
public MD5MD5CRC32FileChecksum getFileChecksum(String src, long length) throws IOException {
    MD5MD5CRC32FileChecksum ret = super.getFileChecksum(src, length);
    if (ret.getChecksumOpt().getBytesPerChecksum() == 0) {
        FileState fileState = getFileState(src);
        if (fileState instanceof CompactFileState) {
            try {
                // Get original checksum for small file.
                byte[] bytes = getXAttr(src, SmartConstants.SMART_FILE_CHECKSUM_XATTR_NAME);
                ret = new MD5MD5CRC32FileChecksum();
                ret.readFields(new DataInputStream(new ByteArrayInputStream(bytes)));
            } catch (IOException e) {
                throw new IOException("Failed to get checksum for SSM Small File: " + e.getMessage());
            }
        }
    }
    return ret;
}
Also used : NormalFileState(org.smartdata.model.NormalFileState) CompactFileState(org.smartdata.model.CompactFileState) FileState(org.smartdata.model.FileState) CompressionFileState(org.smartdata.model.CompressionFileState) MD5MD5CRC32FileChecksum(org.apache.hadoop.fs.MD5MD5CRC32FileChecksum) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) CompactFileState(org.smartdata.model.CompactFileState)

Aggregations

CompactFileState (org.smartdata.model.CompactFileState)19 CompressionFileState (org.smartdata.model.CompressionFileState)15 FileState (org.smartdata.model.FileState)14 NormalFileState (org.smartdata.model.NormalFileState)8 ArrayList (java.util.ArrayList)5 Gson (com.google.gson.Gson)3 IOException (java.io.IOException)3 Path (org.apache.hadoop.fs.Path)3 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)3 FileContainerInfo (org.smartdata.model.FileContainerInfo)3 TypeToken (com.google.gson.reflect.TypeToken)2 BlockLocation (org.apache.hadoop.fs.BlockLocation)2 FileStatus (org.apache.hadoop.fs.FileStatus)2 LocatedFileStatus (org.apache.hadoop.fs.LocatedFileStatus)2 HdfsLocatedFileStatus (org.apache.hadoop.hdfs.protocol.HdfsLocatedFileStatus)2 HdfsNamedFileStatus (org.apache.hadoop.hdfs.protocol.HdfsNamedFileStatus)2 LocatedBlocks (org.apache.hadoop.hdfs.protocol.LocatedBlocks)2 CompactFileStateProto (org.smartdata.protocol.ClientServerProto.CompactFileStateProto)2 CompressionFileStateProto (org.smartdata.protocol.ClientServerProto.CompressionFileStateProto)2 S3FileStateProto (org.smartdata.protocol.ClientServerProto.S3FileStateProto)2